Posted By

MMDeveloper on 09/16/08


Tagged

mysql database class php object


Versions (?)

Who likes this?

13 people have marked this snippet as a favorite

mrjthethird
Scooter
kernelpanics
rezzz
Nix
nb109
umang_nine
weblogics
g33kish
dhavalpatel
shalomfriss
rmethod
ringo380


OOP MySQL Database Class


 / Published in: PHP
 

URL: http://www.mechanicmatt.com

Simple database class. Run queries, obtain OOP results, etc.

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

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: phaberest on September 5, 2012

It gives error for a non-object call on line 83

You need to login to post a comment.