Posted By

MMDeveloper on 02/24/09


Tagged

database class php object oop odbc


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
Scooter
rezzz


OOP ODBC Database Class


 / Published in: PHP
 

Similiar to my other database classes (mysql, mysqli, mssql) but converted to use ODBC

  1. <?php
  2.  
  3. /*
  4. untested
  5. */
  6.  
  7. class database_odbc {
  8.  
  9. var $host = NULL;
  10. var $username = NULL;
  11. var $password = NULL;
  12. var $databaseName = NULL;
  13. var $link = NULL;
  14. var $queries = NULL;
  15. var $errors = NULL;
  16.  
  17. function database_odbc($host, $username, $password) {
  18. $this->host = $host;
  19. $this->username = sha1($username);
  20. $this->password = sha1($password);
  21. $this->link = "";
  22. $this->queries = array ();
  23. $this->errors = array ();
  24.  
  25. $this->link = odbc_connect($this->host, $username, $password);
  26. }
  27.  
  28. function justquery($sql) {
  29. $this->queries[] = $sql;
  30. return odbc_exec($this->link, $sql);
  31. }
  32.  
  33. function loadResult($sql) {
  34. if (!($cur = $this->justquery($sql))) {
  35. return null;
  36. }
  37. $ret = null;
  38. if ($row = odbc_fetch_row( $cur )) {
  39. $ret = $row[0];
  40. }
  41. odbc_free_result( $cur );
  42. return $ret;
  43. }
  44.  
  45. function loadFirstRow($sql) {
  46. if (!($cur = $this->justquery($sql))) {
  47. return null;
  48. }
  49. $ret = null;
  50. if ($row = odbc_fetch_object( $cur )) {
  51. $ret = $row;
  52. }
  53. odbc_free_result( $cur );
  54. return $ret;
  55. }
  56.  
  57. function insertid() {
  58. //return mysql_insert_id( $this->link );
  59. }
  60.  
  61. function query($sql, $key = "", $returns = true, $batch = false) {
  62. $sqls = $result = array ();
  63.  
  64. switch ($batch) {
  65. default:
  66. case true:
  67. foreach ($sql as $index => $query) {
  68. $this->queries[] = $query;
  69. $answer = odbc_exec($this->link, $query);
  70.  
  71. if (!$answer) {
  72. $this->errors[] = odbc_errormsg($this->link);
  73. }
  74. else {
  75. if ($returns != false) {
  76. if (odbc_num_rows($answer) > 0){
  77. while ($row = odbc_fetch_object($answer)) {
  78. if ($key != ""){
  79. $result[$index][$row->$key] = $row;
  80. }
  81. else {
  82. $result[$index][] = $row;
  83. }
  84. }
  85. } else {}
  86. } else {}
  87. }
  88. }
  89. break;
  90.  
  91. case false:
  92. $this->queries[] = $sql;
  93. $answer = odbc_exec($this->link, $sql);
  94.  
  95. if (!$answer) {
  96. $this->errors[] = odbc_errormsg($this->link);
  97. $result = false;
  98. }
  99. else {
  100. if ($returns != false) {
  101. if (odbc_num_rows($answer) > 0){
  102. while ($row = odbc_fetch_object($answer)) {
  103. if ($key != ""){
  104. $result[$row->$key] = $row;
  105. }
  106. else {
  107. $result[] = $row;
  108. }
  109. }
  110. }
  111. else {
  112. return odbc_errormsg($this->link);
  113. }
  114. }
  115. else {
  116. $result = true;
  117. }
  118. }
  119. break;
  120. }
  121.  
  122. return $result;
  123. }
  124.  
  125. function loadObject( $sql, &$object ) {
  126. if ($object != null) {
  127. if (!($cur = $this->justquery($sql))) {
  128. return false;
  129. } else {}
  130. if ($array = odbc_fetch_array( $cur )) {
  131. odbc_free_result( $cur );
  132. $this->bindArrayToObject( $array, $object);
  133. return true;
  134. }
  135. else {
  136. return false;
  137. }
  138. }
  139. else {
  140. if ($cur = $this->justquery($sql)) {
  141. if ($object = odbc_fetch_object( $cur )) {
  142. odbc_free_result( $cur );
  143. return true;
  144. }
  145. else {
  146. $object = null;
  147. return false;
  148. }
  149. }
  150. else {
  151. return false;
  152. }
  153. }
  154. }
  155.  
  156. function bindArrayToObject( $array, &$obj) {
  157. if (!is_array( $array ) || !is_object( $obj )) {
  158. return (false);
  159. }
  160.  
  161. foreach (get_object_vars($obj) as $k => $v) {
  162. if( substr( $k, 0, 1 ) != '_' ) {
  163. $ak = $k;
  164. if (isset($array[$ak])) {
  165. $obj->$k = $array[$ak];
  166. }
  167. }
  168. }
  169.  
  170. return true;
  171. }
  172.  
  173. function formatCSVCell($data) {
  174. $useQuotes = false;
  175.  
  176. $quotable = array (
  177. "\"" => "\"\"",
  178. "," => ",",
  179. "\n" => "\n"
  180. );
  181.  
  182. foreach ($quotable as $char => $repl) {
  183. if (eregi($char, $data)) {
  184. $useQuotes = true;
  185. } else {}
  186. }
  187.  
  188. if ($useQuotes == true) {
  189. foreach ($quotable as $char => $repl) {
  190. $data = str_replace($char, $repl, $data);
  191. }
  192.  
  193. $data = "\"" . $data . "\"";
  194. }
  195. else {
  196.  
  197. }
  198.  
  199. return $data;
  200. }
  201. }
  202. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: materix on July 8, 2012

Nice class. But usage-instructions are a must.

You need to login to post a comment.