| 
<?php
//******************************************************************************************************
 //    Name: sql.php
 //    Revision: 2.0
 //    Date: May 22, 2009
 //    Link: http://doc776.org
 //    Developer: Mikhail Davtian - originaly written by Joe
 //    Description: MySQL database ease-of-use class.
 //******************************************************************************************************
 
 class SQL
 {
 protected $host = 'localhost';
 protected $user = 'username';
 protected $pass = 'password';
 protected $db = 'table';
 private $qr;
 
 //connect to mysql database
 function SQL()
 {
 //connect or die trying
 $this->conn = @mysql_connect($this->host, $this->user, $this->pass)
 OR die('<h1>cannot connect to MYSQL Database</h1><p>Contact <a href="mailto:[email protected]">jrgp</a> to hopefully get this fixed soon</p>');
 
 //select database or die after trying
 @mysql_select_db($this->db,$this->conn)
 OR die('cannot select database');
 
 //set current number of queries to 0
 $this->num_queries = 0;
 }
 
 function query($q, $care = 1, $buffered = 0)
 {
 //check if connected
 if(!$this->conn)
 return false;
 
 //check if the placeholder is currently being used, if it is, unset it.
 if($this->qr != NULL)
 unset($this->qr);
 
 //buffered query?
 if(empty($buffered))
 $this->qr = @mysql_query(trim($q),$this->conn) or $this->query_error($care, $q);
 else
 $this->qr = @mysql_unbuffered_query(trim($q),$this->conn) or $this->query_error($care, $q);
 
 //increase number of queries
 $this->num_queries++;
 
 //if not dead, return result
 return $this->qr;
 }
 
 //query error handler
 function query_error($care, $q)
 {
 //we don't care if it failed
 if (empty($care))
 return true;
 
 //we do, send out message and stop script
 
 //take away anything that was to be send out before this error
 do ob_end_clean();
 while (ob_get_level() !== 0);
 
 //echo the error message
 echo '<h1>Mysql Error</h1>';
 echo '<p>Contact Admin asap to get this fixed.</p>';
 echo '<h2>Error:</h2>';
 echo '<pre><code>'.mysql_error($this->conn).'</code></pre>';
 echo '<h2>Query:</h2>';
 echo '<pre><code>'.$q.'</code></pre>';
 
 //close connection to mysql database
 $this->close();
 
 //prevent script from continuing
 exit;
 }
 
 //gets associative, or number array or an object or full result in associative array from a mysql dataset
 function data($q, $t = null)
 {
 if ($t == 1)
 return mysql_fetch_row($q);
 elseif ($t == 2)
 return mysql_fetch_object($q);
 elseif ($t == 3) {
 while($r = mysql_fetch_assoc($q))
 $ar[] = $r;
 return $ar;
 }
 else
 return mysql_fetch_assoc($q);
 }
 
 //gets number of rows found from a query
 function num($q)
 {
 return mysql_num_rows($q);
 }
 
 //gets the id of last insert
 function lastid()
 {
 return mysql_insert_id($this->conn);
 }
 
 //adds slashes to string to protect against sql injection attempts
 function prot($str)
 {
 //if the string in question just contains digits, no other characters, such as quotes or letters, don't do anything to it, otherwise, escape it
 return @ctype_digit($str) ? $str : mysql_real_escape_string($str,$this->conn);
 }
 
 //free memory used by a query
 function free($q)
 {
 mysql_free_result($q);
 }
 
 function freelast()
 {
 if (!empty($this->qr))
 mysql_free_result($this->qr);
 }
 
 //return the number if affected rows in the last UPDATE
 function aff()
 {
 return mysql_affected_rows($this->conn);
 }
 
 //close connection to database
 function close()
 {
 //check if already closed
 if (!$this->conn)
 return true;
 
 //close it
 mysql_close($this->conn);
 
 //set that it is closed
 $this->conn = FALSE;
 
 //unset variable
 unset($this->qr);
 }
 
 //auto connection close
 /*function __destruct()
 {
 if ($this->conn)
 $this->close();
 }*/
 }
 ?>
 
 |