Posted By

MMDeveloper on 02/24/09


Tagged

database class php object oop mysqli


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

nickelstar
umang_nine
weblogics


OOP MySQLi Database Class


 / Published in: PHP
 

URL: http://www.mechanicmatt.com

Similar to my OOP MySQL class, but converted to use the OOP style of MySQLi

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

Report this snippet  

You need to login to post a comment.