MySQL database class

I'm using a database class for a while now, and I must say it works fantastic. It facilitates in my PHP/MySQL development by aiding in debugging and error reporting. DbConnect will automatically close the connection when the page thread is finished. The class is PHP 4 proof.

Example use:

  1. //Open connection
  2. $dbConnection = new dbConnect();
  3.  
  4. //Send a MySQL query
  5. $dbConnection->query("SELECT * FROM table");
  6.  
  7. //Fetch a row
  8. $row = $dbConnection->fetchRow();
  9.  
  10. //Loop through all rows
  11. while($row = $dbConnection->fetchRow()) { }
  12. //If you know the query will return 1 record, fetch the cell via
  13. $value = $dbConnection->fetchFirstCell();
  14.  
  15. $result = $dbConnection->result; //Return the complete result
  16. $count = $dbConnection->affectedRows; //Return the affected rows count in previous MySQL operation
  17. $id = $dbConnection->insertID; //Return the ID generated from the previous INSERT operation

Read more for the complete class.

Class dbConnect:

  1. class dbConnect {      
  2.     var $connection;
  3.     var $result;
  4.     var $affectedRows;
  5.     var $insertID;
  6.     
  7.     function dbConnect() {
  8.         $this->connection = mysql_connect(db_host, db_user, db_pass);
  9.         mysql_select_db(db_name);
  10.         register_shutdown_function(array(&$this, "close"));
  11.     }
  12.     
  13.     function close() {
  14.         mysql_close($this->connection);
  15.     }
  16.     
  17.     function query($query, $debug = false) {            
  18.         $this->result = mysql_query($query) or die();
  19.         $this->affectedRows = mysql_affected_rows();
  20.         $this->insertID = mysql_insert_id();
  21.         
  22.         if($debug) {
  23.             echo "<fieldset><legend>SQL-Query in <i>".$_SERVER["SCRIPT_NAME"]."</i></legend>".PMA_SQP_formatHtml(PMA_SQP_parse($query))."</fieldset>";
  24.             if(eregi("select", $query)) {
  25.                 echo "<fieldset><legend>SELECT result: ".$this->affectedRows." row(s)</legend><table cellspacing="0">";
  26.                 for($i = 0; $row = $this->fetchRow("assoc", $this->result); $i++) {
  27.                     if($i == 0) {
  28.                         echo "<tr>";
  29.                         foreach($row as $key => $value) echo "<th>$key</th>";
  30.                         echo "</tr>";
  31.                     }
  32.                     echo "<tr>";
  33.                     foreach($row as $value) echo "<td>$value</td>";
  34.                     echo "</tr>";
  35.                 }
  36.                 echo "</table></fieldset>";
  37.             }
  38.         }
  39.     }
  40.     
  41.     function fetchFirstCell() {
  42.         $row = mysql_fetch_row($this->result);
  43.         return $row[0];
  44.     }
  45.     
  46.     function fetchRow($type = "assoc", $result = null, $debug = false) {
  47.         switch($type){
  48.             case "assoc":
  49.                 return mysql_fetch_array(($result ? $result : $this->result), MYSQL_ASSOC);
  50.                 break;
  51.             case "num":
  52.                 return mysql_fetch_array(($result ? $result : $this->result), MYSQL_NUM);
  53.                 break;
  54.             case "both";
  55.                 return mysql_fetch_array(($result ? $result : $this->result), MYSQL_BOTH);
  56.                 break;
  57.             case "obj":
  58.                 return mysql_fetch_object($result ? $result : $this->result);
  59.                 break;
  60.         }
  61.     }
  62.     
  63.     function freeResult() {
  64.         mysql_free_result($this->result);
  65.     }
  66. }

Don't forget to define db_host, db_user, db_pass and db_name in your configuration file.

The PMA_SQP_formatHtml and PMA_SQP_parse are the same functions used in PHPMyAdmin to format a query. I will post these later.

Author rcz Time Jul 11, 2007 Comment on 'MySQL database class' Add comment Bookmark 'MySQL database class' Bookmark Permalink to 'MySQL database class' Permalink
Bookmark 'MySQL database class' to Netvouz Bookmark 'MySQL database class' to Facebook Bookmark 'MySQL database class' to Furl Bookmark 'MySQL database class' to Technorati Bookmark 'MySQL database class' to Stumble Upon Bookmark 'MySQL database class' to Digg Bookmark 'MySQL database class' to Del.icio.us

Comments (2)

Comment author fatjoe Time Oct 25, 2007 06:03 GMT #1
Don't use eregi
Comment author Lss Comment author email lss<at>gmail.com Time Jul 8, 2008 09:58 GMT #2
ssstestFace: Kiss

Add comment

Your name
Your email or website (optional)
Icon
Confirmation code
Confirmation code
Message
face:happy face:glasses face:devilgrin face:crying face:grin face:laughing face:sad face:kiss face:shocked face:wink face:monkey face:angel