My database class


/ Published in: PHP
Save to your folder(s)

Just my simple db class i use


Copy this code and paste it in your HTML
  1. <?php
  2. /*****************
  3.  
  4. SIMPLE DATABASE CLASS
  5. http://www.onitindustries.com
  6.  
  7. ----------------------
  8.  
  9. HOW TO USE
  10.  
  11. ----------------------
  12. // Calling the class and connecting
  13.  
  14. $DB = new db(); // start database class
  15. $DB->connect($c_db_username, $c_db_password, $c_db_database, $c_db_host); // connect to the database
  16.  
  17. // Example Query - ARRAY
  18. $results = db::query("SELECT * FROM table", DB_GET_ARRAY);
  19. foreach($results as $result) {
  20.  
  21. echo $result->column;
  22.  
  23. }
  24.  
  25. ******************/
  26.  
  27. // DEFINITIONS
  28. define("DB_GET_ROW", "getRow");
  29. define("DB_GET_ARRAY", "fetchArray");
  30.  
  31. class db {
  32.  
  33. private $username;
  34. private $password;
  35. private $database;
  36. private $host;
  37. private $status;
  38. private $con;
  39.  
  40. public static $_query;
  41. public static $errors = array();
  42.  
  43. function __construct() {
  44.  
  45. }
  46.  
  47. // connect to the database
  48. function connect($username, $password, $database, $host = 'localhost')
  49. {
  50.  
  51. $this->username = $username;
  52. $this->password = $password;
  53. $this->database = $database;
  54. $this->host = $host;
  55.  
  56. $con = mysql_connect(''.$this->host.'', ''.$this->username.'', ''.$this->password.'');
  57.  
  58. $select = mysql_select_db($this->database, $con);
  59.  
  60. if(!$con) {
  61.  
  62. $this->errors[] = 'Could not connect to the database';
  63.  
  64. } elseif(!$select) {
  65.  
  66. $this->errors[] = 'Could not select the database';
  67.  
  68. } else {
  69.  
  70. $this->con = $con;
  71. }
  72.  
  73. return ;
  74.  
  75. }
  76.  
  77. // Close connection
  78. public function disconnect()
  79. {
  80. mysql_close($this->con);
  81. }
  82.  
  83. public function showStatus() {
  84.  
  85. if(!$this->status) {
  86.  
  87. foreach($this->errors as $value) {
  88.  
  89. echo $value.'<br>';
  90.  
  91. }
  92.  
  93. } else {
  94.  
  95. echo 'all good';
  96. }
  97.  
  98. }
  99.  
  100. // process queries
  101. public static function query($sql, $queryType='')
  102. {
  103. db::$_query = @mysql_query($sql);
  104.  
  105. switch($queryType)
  106. {
  107.  
  108. case 'getRow':
  109. return self::getRow(db::$_query);
  110. break;
  111.  
  112. case 'fetchArray':
  113. return self::fetchArray(db::$_query);
  114. break;
  115.  
  116. case 'getCount':
  117. return self::fetchArray(db::$_query);
  118. break;
  119.  
  120. default: '';
  121. }
  122.  
  123. return (self::$_query) ? true : false;
  124. }
  125.  
  126. // get single row
  127. function getRow($query='')
  128. {
  129. if($query) { self::$_query = $query; }
  130.  
  131. return @mysql_fetch_row(self::$_query);
  132. }
  133.  
  134. // get array
  135. function fetchArray($query='')
  136. {
  137. if($query) { self::$_query = $query; }
  138.  
  139. while($row = @mysql_fetch_array(self::$_query))
  140. {
  141. $result[] = $row;
  142. }
  143.  
  144. return $result;
  145. }
  146.  
  147. // get last insert id
  148. public static function last_insert_id()
  149. {
  150. return mysql_insert_id();
  151. }
  152.  
  153. }
  154.  
  155. ?>

URL: http://www.onitindustries.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.