Posted By

ishkur on 07/22/08


Tagged

database class php5


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

romanos
vali29


database class


 / Published in: PHP
 

it's still a work in progress. There are a lot of things I would like to do with it, but I think how I have it set up now is pretty good for how i need it.

It's essentially the backbone of a CMS project I have going (PaamayimCMS).

One major feature that I would LOVE to add to it is chainability. I've been looking at the P4A project, and they claim to have a database wrapper that functions in a similar way to jQuery in regards to that.

In due time i suppose.

  1. <?php
  2.  
  3. class DB {
  4.  
  5. //connection variables
  6. protected $dbhost = 'localhost';
  7. protected $dbuser = 'root';
  8. protected $dbpass = '';
  9. protected $dbname = 'database';
  10.  
  11. //normal global resources
  12. protected $db = null;
  13. protected static $instance = null;
  14.  
  15. /**
  16. * Singleton pattern for instantiating the DB class
  17. */
  18. public static function GetInstance()
  19. {
  20. if(!self::$instance)
  21. {
  22. self::$instance = new DB;
  23. }
  24. return self::$instance;
  25. }
  26.  
  27. protected function __construct()
  28. {
  29. if(($db = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass)) === false)
  30. {
  31. trigger_error('There was an error connecting to the server.');
  32. }
  33.  
  34. if((@mysql_select_db($this->dbname, $db)) === false)
  35. {
  36. trigger_error('There was an error selecting the database.');
  37. }
  38. $this->db = $db;
  39. }
  40.  
  41. /**
  42. * simple method for sanitizing SQL query strings.
  43. */
  44. public function Clean($string)
  45. {
  46. {
  47. $string = stripslashes($string);
  48. }
  49. return mysql_real_escape_string($string);
  50. }
  51.  
  52. /**
  53. * DB class method call to the Query child class.
  54. * SELECT queries only
  55. * Returns number of rows affected.
  56. */
  57. public function Query($statement)
  58. {
  59. return new Query($statement, $this->db);
  60. }
  61.  
  62. /**
  63. * DB class method call to the Execute child class.
  64. * INSERT/UPDATE/DELETE queries only
  65. * Returns number of rows affected.
  66. */
  67. public function Execute($statement)
  68. {
  69. return new Execute($statement, $this->db);
  70. }
  71.  
  72. /**
  73. * Used to get the last inserted record
  74. */
  75. public function InsertID()
  76. {
  77. return mysql_insert_id($this->link);
  78. }
  79.  
  80. public function __destruct()
  81. {
  82. @mysql_close($this->db);
  83. }
  84.  
  85. }
  86.  
  87. /**
  88.  * These are separate classes for the reason that I would like
  89.  * to be able to edit them with as little distruption to the
  90.  * flow of the parent class as possible.
  91.  */
  92.  
  93. /**
  94.  * Query child class. Used for running SELECT queries.
  95.  * Called exclusively by the DB parent class in the Query method
  96.  */
  97. class Query
  98. {
  99. protected $result;
  100.  
  101. public function __construct($statement, $link)
  102. {
  103. if(($this->result = mysql_query($statement, $link)) === false)
  104. {
  105. trigger_error('There was an error in processing the Query method: '. mysql_error());
  106. }
  107. }
  108.  
  109. /**
  110. * I dont really like how this class is going so far. its almost
  111. * emulating the regular set of mysql_fetch_*() functions.
  112. *
  113. * So, im going to try and implement some sort of method where
  114. * i can specify the fetch action as an argument.
  115. * EG. $result = $db->Query($query)->Fetch('object', 'class');
  116. * Or $arr = $db->Query($query)->Fetch('assoc');
  117. */
  118. public function Get($method, $class = null)
  119. {
  120. $sql_magic = "mysql_fetch_".$method;
  121. if(isset($class))
  122. {
  123. return @$sql_magic($this->result, $class);
  124. } else {
  125. return @$sql_magic($this->result);
  126. }
  127. }
  128.  
  129. public function NumRows()
  130. {
  131. return @mysql_num_rows($this->link);
  132. }
  133.  
  134. public function __destruct()
  135. {
  136. @mysql_free_result($this->result);
  137. }
  138. }
  139.  
  140. /**
  141.  * Execute child class. Used for running INSERT/UPDATE/DELETE queries.
  142.  * Called exclusively by the DB parent class in the Execute method
  143.  */
  144. class Execute
  145. {
  146. public function __construct($statement, $link)
  147. {
  148. if((@mysql_query($statement, $link)) == false)
  149. {
  150. trigger_error('There was an error processing the Execute method: '. mysql_error());
  151. }
  152. return @mysql_affected_rows($link);
  153. }
  154. }
  155.  
  156. class Paginate
  157. {
  158. /**
  159. * Adding this so that I can have pagination included in the class
  160. *
  161. * Its gonna be a bitch though.
  162. */
  163. }
  164.  
  165. ?>

Report this snippet  

You need to login to post a comment.