So this is my mysql wrapper class witch adds some features currently not found in the original php driver functions... such as prepared statements. This is still a work in progress as there is a few more query helping functions i would like to add still but those are not the biggest need.

class tp_database{

  public $error            = "";
  public $errno            = 0;
  protected $query_count   = 0;
  protected $lid           = 0;
  protected $qid           = 0;
  protected $debug         = false;
  protected $statment	   = array();



  public function __construct($server , $user , $pass , $db){
  		$this->_server   = $server;
		$this->_user     = $user;
		$this->_pass     = $pass;
		$this->_db	     = $db;
  }



  public function connect(){

  		$lid = mysql_connect($this->_server , $this->_user , $this->_pass);
		$sdb = mysql_select_db($this->_db , $lid);
		if(!isset($lid)){

			$this->error("<div style='text-align:center'>" 
					   . "<span style='padding: 5px; border: 1px solid #999; background-color:#EFEFEF;" 
					   . "font-family: Verdana; font-size: 11px;'>" 
					   . "<b>Database Error:</b>Connection to Database " . $this->_db . " Failed</span></div>");
		}

		if(!isset($sdb)){
			$this->error("<div style='text-align:center'>" 
					   . "<span style='padding: 5px; border: 1px solid #999; background-color: #EFEFEF;" 
					   . "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>" 
					   . "<b>Database Error:</b>mySQL database (" . $this->_db . ")cannot be used</span></div>");
		}

		$this->lid = $lid;

		unset($this->_pass);
		unset($this->_user);
  }



  public function real_escape($string){
  		if(is_array($string)){
			foreach($string as $k => $v){
				$string[mysql_real_escape_string($k)] = mysql_real_escape_string($v);
			}
		}

		$string = mysql_real_escape_string($string);

		return $string;
  }



  public function escape($string){
  		$string = clean($string);
		return $string;
  }

  public function query($statment){
  		return $query = $this->prepare($statment)->execute();
  }



  public function prepare($statment){
  		if(is_null($statment)){
			return;
		}

		$args = func_get_args();
		array_shift($args);

		if(isset($args[0]) && is_array($args[0])){
			$args = $args[0];
		}

		$statment = str_replace( "'%s'", '%s', $statment ); 
		$statment = str_replace( '"%s"', '%s', $statment ); 
		$statment = preg_replace( '|(?<!%)%s|', "'%s'", $statment ); 
		array_walk( $args, array( &$this, 'real_escape' ) );
		$this->query_count++;
		$count = $this->query_count;
		$this->statment[$count] = @vsprintf( $statment, $args );
		return $this;
  }



  public function execute(){
  		$count = $this->query_count;
  		$stmt = $this->statment[$count];
		$this->last_query = $stmt;
		$qid = mysql_query($stmt , $this->lid);
		if(!isset($qid)){
			$this->error('Database Error: there seems to be a query error');
		}

		return $this->qid = $qid;
  }



  function insert($table = null, $data){
	  if ($table === null or empty($data) or !is_array($data)) {
		  $this->error("Invalid array for table: <b>".$table."</b>.");
		  return false;
	  }

	  $q = "INSERT INTO `" . $table . "` ";
      $v = '';
      $k = '';

      foreach ($data as $key => $val) {

          $k .= "`$key`, ";
          if (strtolower($val) == 'null'){
		  	  $v .= "NULL, ";
		  }elseif (strtolower($val) == 'now()'){
              $v .= "NOW(), ";
          }elseif (strtolower($val) == 'tzdate'){
              $v .= "DATE_ADD(NOW(),INTERVAL " . date_default_timezone_get() . " HOUR), ";
          }else{
		  	  $v .= "'" . $this->escape($val) . "', ";
		  }

      }

      $q .= "(" . rtrim($k, ', ') . ") VALUES (" . rtrim($v, ', ') . ");";

      if ($this->query($q)) {
          return $this->insertid();
      }else{
	  		return false;
	  }

  }

   function update($table = null , array $data , $where = '1'){
	  if ($table === null or empty($data) or !is_array($data)) {
		  $this->error("Invalid array for table: <b>" . $table . "</b>.");
		  return false;
	  }

	  $q = "UPDATE `" . $table . "` SET ";
      foreach ($data as $key => $val) {
          if (strtolower($val) == 'null'){
              $q .= "`$key` = NULL, ";
          }elseif (strtolower($val) == 'now()'){
              $q .= "`$key` = NOW(), ";
          }elseif (strtolower($val) == 'tzdate'){
              $q .= "`$key` = DATE_ADD(NOW(),INTERVAL " . date_default_timezone_get() . " HOUR), ";
          }elseif (strtolower($val) == 'default()'){
              $q .= "`$key` = DEFAULT($val), ";
          }elseif(preg_match("/^inc\((\-?\d+)\)$/i",$val,$m)){
              $q.= "`$key` = `$key` + $m[1], ";
          }else{
              $q .= "`$key`='" . $this->escape($val) . "', ";
		  }
     }
      $q = rtrim($q, ', ') . ' WHERE ' . $where . ';';

      return $this->query($q);
  }



  function delete($table , $where = ''){
	  $q = !$where ? 'DELETE FROM ' . $table : 'DELETE FROM ' . $table . ' WHERE ' . $where;
      return $this->query($q);
  }



  function free($query_id = -1){
      if ($query_id != -1){
          $this->qid = $query_id;
      }

      return mysql_free_result($this->qid);
  }



  public function fetch_array(){
  		while($result = mysql_fetch_array($this->qid)){
			return $result;
		}

		return false;
  }



  public function fetch_object($classname = null , array $params = array()){
  		if(is_null($classname)){
			while($result = mysql_fetch_object($this->qid)){
				return $result;
			}
		}else{
			while($result = mysql_fetch_object($this->qid , $classname , $params = array())){
				return $result;
			}
		}

		return false;
  }

  function insert_id(){
      return mysql_insert_id($this->lid);
  }

  function affected() {
	  return mysql_affected_rows($this->lid);
  }

  function debug($debug_mode = false){
  	 return $this->debug = $debug_mode;
  }

  function numrows($query_id = -1){
      if ($query_id != -1)
          $this->qid = $query_id;

	  $this->num_rows = mysql_num_rows($this->qid);
      return $this->num_rows;
  }

  public function error($msg = ''){

      if ($this->lid > 0) {
          $this->error_desc = mysql_error($this->lid);
          $this->error_no = mysql_errno($this->lid);
      } else {
          $this->error_desc = mysql_error();
          $this->error_no = mysql_errno();
      }

      $error = "<div style=\"background-color:#FFF; border: 3px solid #999; padding:10px\">";
      $error .= "<b>mySQL WARNING!</b><br />";
      $error .= "Database Error: $msg <br /> More Information: <br />";
      $error .= "<ul>";
      $error .= "<li> Mysql Error : " . $this->error_no . "</li>";
      $error .= "<li> Mysql Error no # : " . $this->error_desc . "</li>";
      $error .= "<li> Date : " . date("F j, Y, g:i a") . "</li>";
      $error .= "<li> Referer: " . isset($_SERVER['HTTP_REFERER']) . "</li>";
      $error .= "<li> Script: " . $_SERVER['REQUEST_URI'] . "</li>";
      $error .= '</ul>';
      $error .= '</div>';
      exit($error);
  }
}

    Having this use [man]PDO[/man] might allow a bit of streamlining.

      i would agree but if you use PDO there is really no need for a wrapper class or a class like this as you could simply use the great PDO features... without having to go through a wrapper as such. but for people who do not have PDO or not using the mysqli extension this class gives the same great features.... to an extent.

      but is there some features im missing or could be added for better usability...

        Roughly speaking, the only people who would (a) not have PDO installed and (b) be capable of using your class, would be those who are using PHP 5.0.x. Earlier versions won't parse your code, and later versions have PDO by default.

        As far as "missing features" that could be added - you could look at the PDO interface to see what it has and you don't (transaction handling, for example).

          In any case, if you have PHP 5.x, then you should have MySQLi (and have had to intentionally installed the older MySQL interface in order to use it.) 😉

          But a few thoughts on the code posted:

          Add PHPDocumentor style comments immediately above each method definition (or at least each public method) with, at a minimum, a brief description, an @return and @ for each input parameter, so that "smart" editors/IDEs can use them for tooltip-type help while coding.

                public function fetch_array(){
                        while($result = mysql_fetch_array($this->qid)){
                          return $result;
                      }
          
                  return false;
            }
          

          There is no need to use a while() loop if you are simply going to return() on the first interation. You could just do:

                public function fetch_array(){
                        return mysql_fetch_array($this->qid));
                }
          
                public function query($statment){
                        return $query = $this->prepare($statment)->execute();
                }
          

          The $query = seems redundant here, since you are then immediately returning it, thus there is no reason to assign the result to $query. The PHP compiler may be smart enough to optimize that out, but then again, it might not be. It's not end-of-the-world stuff, mind you, but it's a simple optimization to include here. 🙂

                  public function escape($string){
                          $string = clean($string);
                        return $string;
                  } 

            It looks as though you haven't finished this method. Also, does it need to be public? It seems more like something that would only be of use to the object itself, in which case declaring it private would be preferable.

                            $this->error('Database Error: there seems to be a query error'); 

            Providing some debugging information could be beneficial.

             $error = "<div style=\"background-color:#FFF; border: 3px solid #999; padding:10px\">";
                      $error .= "<b>mySQL WARNING!</b><br />";
                      $error .= "Database Error: $msg <br /> More Information: <br />";
                      $error .= "<ul>";
                      $error .= "<li> Mysql Error : " . $this->error_no . "</li>";
                      $error .= "<li> Mysql Error no # : " . $this->error_desc . "</li>";
                      $error .= "<li> Date : " . date("F j, Y, g:i a") . "</li>";
                      $error .= "<li> Referer: " . isset($_SERVER['HTTP_REFERER']) . "</li>";
                      $error .= "<li> Script: " . $_SERVER['REQUEST_URI'] . "</li>";
                      $error .= '</ul>';
                      $error .= '</div>';
                      exit($error); 
            

            So instead of turning on a light on the pilot's console and ringing an alarm in the co&#99;kpit, you prefer to indicate a problem by crashing the entire plane in front of the general public?

              Hi!

              Do you mean its logically that a data base wrapper is styling the look of an error message?

                gammaster;10989771 wrote:

                Hi!

                Do you mean its logically that a data base wrapper is styling the look of an error message?

                I believe what Weedpacket was pointing out is that it's probably should not be the DB class's responsibility to exit the application if there is a DB error, nor should the default behavior when there is an error be to display database details to the user in any case.

                  this class is dead i have replaced it with a more up to date class
                  with PDO and some less features. This is a work in progress to replace my mysql class.

                  please this class is a work in progress so some things will change or taken away. and features not needed in this version please let me know or features it's missing.

                  class DBO{
                  
                  const ERR_SILENT            = 0;
                  const ERR_WARNING           = 1;
                  const ERR_EXCEPTION         = 2;
                  
                  const FETCH_ASSOC           = 3;
                  const FETCH_BOTH            = 4;
                  const FETCH_OBJ             = 5;
                  const FETCH_NUM             = 6;
                  const FETCH_CLASS           = 7;
                  
                  private $_error_mode        = 2;
                  private $_error_last        = '';
                  private $_query_last        = '';
                  private $_results_last      = '';
                  private $_affected_rows     = 0;
                  private $_columns_count     = 0;
                  private $_query_count       = 0;
                  private $_results_count     = 0;
                  private $_tables            = array();
                  private $_columns           = array();
                  private $_statements        = array();
                  private $_connect_persitent = false;
                  
                  
                  
                  public function __construct($driver = '' , $dbname = '' , $server = '' , $user = '' , $pass = ''){
                  
                  	$this->_params = array($driver , $dbname , $server , $user , $pass);
                  
                  	foreach($this->_params as $params){
                  		if(!is_string($params)){
                  			if($this->_error_mode === 0){
                  				return false;
                  			}elseif($this->_error_mode === 1){
                  				trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
                  				              .'string type only string types are expected by DB __construct. File:');
                  			}else{
                  				throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
                  				              .'string type only string types are expected by DB __construct. File:');
                  			}
                  		}
                  	}
                  
                  	$this->connect($this->_params[0], $this->_params[1], $this->_params[2],$this->_params[3],$this->_params[4]);
                  }
                  
                  protected function connect($driver , $dbname , $server , $user , $pass){
                  	if(strtolower($driver) === 'mysql'){
                  		try{
                  			$this->_lid = new PDO($driver.':host='.$server.';dbname='.$dbname , $user , $pass);
                  		}catch(PDOException $pdo_error){
                  			if($this->_error_mode === 0){
                  				return false;
                  			}elseif($this->_error_mode === 1){
                  				trigger_error('Database Error: '. $pdo_error->getMessage());
                  				$this->_error_last = array(
                  					'message' => $pdo_error->getMessage() ,
                  					'file'    => __FILE__,
                  					'line'    => __LINE__,
                  
                  				);
                  
                  			}else{
                  				throw new DBO_Exception('Database Error: '. $pdo_error->getMessage());
                  			}
                  		}
                  	}elseif(strtolower($driver) === 'pgsql'){
                  		try{
                  			$this->_lid = new PDO($driver.':dbname='.$dbname.';host='.$server, $user , $pass);
                  		}catch(PDOException $pdo_error){
                  			if($this->_error_mode === 0){
                  				return false;
                  			}elseif($this->_error_mode === 1){
                  				trigger_error('Database Error: '. $pdo_error->getMessage());
                  			}else{
                  				throw new DBO_Exception('Database Error: '. $pdo_error->getMessage());
                  			}
                  		}
                  	}
                  
                  	return $this->_lid;
                  }
                  
                  public function query($statement = '' , array $params = array()){
                  	if(count($params) > 0){
                  		return $this->prepare($statement , $params)->execute();
                  	}else{
                  		return $this->prepare($statement)->execute();
                  	}
                  
                  	return false;
                  }
                  
                  public function prepare($statement = '' , array $params = array()){
                  	if(!is_string($statement)){
                  		if($this->_error_mode === 0){
                  			return false;
                  		}elseif($this->_error_mode === 1){
                  			trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
                  				          .'string type only string types are expected by DB prepare. File:');
                  		}else{
                  			throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
                  				                    .'string type only string types are expected by DB prepare. File:');
                  		}
                  	}
                  
                  	if(!is_array($params)){
                  		if($this->_error_mode === 0){
                  			return false;
                  		}elseif($this->_error_mode === 1){
                  			trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
                  				          .'string type only string types are expected by DB prepare.');
                  		}else{
                  			throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
                  				                    .'string type only string types are expected by DB prepare.');
                  		}
                  	}
                  
                  	$this->_query_count++;
                  	$qid = $this->_query_count;
                  	$this->_statements[$qid] = array($statement , $params);
                  	$this->_statements;
                  	return $this;
                  }
                  
                  
                  
                  public function execute(){
                  	$statement = $this->_statements[$this->_query_count][0];
                  	$params    = $this->_statements[$this->_query_count][1];
                  	if(count($params) > 0){
                  		$transaction = $this->_lid->prepare($statement);
                  		$transaction->execute($params);
                  		$this->_columns_count = $transaction->columnCount();
                  		$this->_affected_rows = $transaction->rowCount();	
                  	}else{
                  		$transaction = $this->_lid->prepare($statement);
                  	    $transaction->execute();
                  		$this->_columns_count = $transaction->columnCount();
                  		$this->_affected_rows = $transaction->rowCount();	
                  	}
                  
                  	$this->_statements[$this->_query_count] = $transaction;
                  	return $this;
                  }
                  
                  public function insert($table , array $columns = array() , array $values = array()){
                  	$qs = "INSERT INTO ".$table;
                  	$qs.= "('".implode("','" , $columns)."')";
                  	$qs.= "VALUES('".implode("','" , $values)."')";
                  	$qs.= ";";
                  	$query = $this->prepare($qs)->execute();
                  }
                  
                  public function fetch($fetch_mode = self::FETCH_BOTH){
                  	$transaction = $this->_statements[$this->_query_count];
                  	$fetch_modes = array(
                  		3 => PDO::FETCH_ASSOC,
                  		4 => PDO::FETCH_BOTH ,
                  		5 => PDO::FETCH_OBJ ,
                  		6 => PDO::FETCH_NUM ,
                  		7 => PDO::FETCH_CLASS
                  	);
                  
                  	$results = $transaction->fetch($fetch_modes[$fetch_mode]);
                  	$this->_results_count = count($results);
                  	return $results;
                  }
                  
                  public function fetchAll(){
                  	$transaction = $this->_statements[$this->_query_count];
                  	$results = $transaction->fetchAll();
                  	$this->_results_count = count($results);
                  	return $results;
                  }
                  
                  public function fetchObject(){
                  	$transaction = $this->_statements[$this->_query_count];
                  	$results = $transaction->setFetchMode(PDO::FETCH_OBJECT);
                  	return $results;
                  }
                  
                  public function fetchClass($classname , array $args = array()){
                  	$transaction = $this->_statements[$this->_query_count];
                  
                  	if(!is_array($args)){
                  		if($this->_error_mode === 0){
                  			return false;
                  		}elseif($this->_error_mode === 1){
                  			trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
                  				          .'string type only string types are expected by DB prepare.');
                  		}else{
                  			throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
                  				                    .'string type only string types are expected by DB prepare. File:');
                  		}
                  	}elseif(count($args) > 0){
                  		$results = $transaction->setFetchMode(PDO::FETCH_CLASS , (string)$classname , (array) $args);
                  	}else{
                  		$results = $transaction->setFetchMode(PDO::FETCH_CLASS , (string)$classname);
                  	}
                  
                  	return $results;
                  }
                  
                  public function getNumColumns(){
                  	return $this->_columns_count;
                  }
                  
                  public function getNumResults(){
                  	return $this->_results_count;
                  }
                  
                  public function getAffectedRows(){
                  	return $this->_affected_rows;
                  }
                  
                  public function getLastError(){
                  	return $this->_last_error;
                  }
                  
                  public function getErrorMode(){
                  	return $this->_error_mode;
                  }
                  
                  public function queryCount(){
                  	return $this->_query_count;
                  }
                  
                  public function setAttributes(){
                  	$args = func_get_args();
                  	foreach($args as $arg){
                  		 $this->setPersistentConnection($arg);
                  		 $this->setErrorMode($arg);
                  	}
                  
                  	return;
                  }
                  
                  public function setErrorMode($error_mode){
                  	if(is_int($error_mode)){
                  		if($error_mode === 0 || $error_mode === 1 || $error_mode === 2){
                  			return $this->_error_mode = (int)$error_mode;
                  		}
                  	}
                  
                  	return false;
                  }
                  
                  }
                  
                  class DBO_Exception extends Exception{
                  
                  }
                  
                  try{
                  	$db = new DBO('mysql' , 'testdb' , 'localhost' , 'user' , 'pass');
                  	var_dump($db->insert('users' , array('firstname' , 'lastname') , array('josh' , 'getner')));
                  	$db->prepare('SELECT * FROM users')->execute();
                  	$res = $db->fetch(DBO::FETCH_ASSOC);
                  	var_dump($res);
                  
                  }catch(DBO_Exception $dboerror){
                  	echo $dboerror->getMessage();
                  }
                  
                    2 months later

                    VigRX Plus 5 Month Supply Man's Enhancemnt Penis Enlargement Pills Massive Provident, Set-up At present! It is really an amazing Sale VigRX Plus 5 Month Give Male Enhancemnt Penis Enlargement Pills.I warmly subscribe to Transaction VigRX Extra 5 Month Supply Manful Enhancemnt Penis Enlargement Pills in place of anyone.I absolutly love it!

                    We Look like and Choose Muffled Prices to tender You Here! Best Offers Today!

                    Allowing for regarding men who longing bigger, harder, longer-lasting erections, there's from time to time VigRX Plus. VigRX Bonus is the penis enhancement work that delivers the most results in the shortest time. You'll bear a larger penis when you're aroused, with greater erection capacity and longer-lasting fleshly performance.

                    Month #1: The first novelty that you can count on to profit from is longer enduring erections. TVigRX And 5 Month Cater to Manful Enhancemnt Penis Enlargement Pills Do not Misunderstand!! For men who pine for bigger, harder, longer-lasting erections, there's at the moment VigRX Plus. VigRX Profit is the penis enhancement output that delivers the most results in the shortest time. You'll prepare a larger penis when you're aroused, with greater erection intelligence and longer-lasting sexual performance.

                    Month #1: The earliest substitution that you can presume to fancy is longer eternal erections. T... VigRX Gain 5 Month Purvey Spear Enhancemnt Penis Enlargement Pills Read More

                    High-pressure Less And Save More,Do not Yearn for Our Best Act on

                    More Details & Take In this day!

                    Cheapest Price VigRX Together with 5 Month Supply Virile Enhancemnt Penis Enlargement Pills

                    Kindest Buy VigRX Addition 5 Month Supply Manful Enhancemnt Penis Enlargement Pills

                    On Marketing VigRX Together with 5 Month Victual Manful Enhancemnt Penis Enlargement Pills

                    Discount VigRX Plus 5 Month Come up with Masculine Enhancemnt Penis Enlargement Pills

                      VigRX Plus 5 Month Supply Virile Enhancemnt Penis Enlargement Pills Jumbo Cache, Order Nowadays! It is truly an awesome Trading VigRX Added 5 Month Contribute Manful Enhancemnt Penis Enlargement Pills.I strongly subscribe to Rummage sale VigRX Plus 5 Month Contribute Male Enhancemnt Penis Enlargement Pills for anyone.I absolutly be wild about it!

                      We Be a match for and Decide Down Prices to tender You Here! Worst Offers Today!

                      Because of men who fancy bigger, harder, longer-lasting erections, there's now VigRX Plus. VigRX Increased by is the penis enhancement product that delivers the most results in the shortest time. You'll be suffering with a larger penis when you're aroused, with greater erection capacity and longer-lasting sexual performance.

                      Month #1: The first novelty that you can presume to profit from is longer undying erections. TVigRX Plus 5 Month Provisioning Manly Enhancemnt Penis Enlargement Pills Do not Misunderstand!! For men who want bigger, harder, longer-lasting erections, there's at the moment VigRX Plus. VigRX Profit is the penis enhancement by-product that delivers the most results in the shortest time. You'll prepare a larger penis when you're aroused, with greater erection right stuff and longer-lasting earthy performance.

                      Month #1: The ahead change that you can wait for to take is longer lasting erections. T... VigRX Plus 5 Month Give Male Enhancemnt Penis Enlargement Pills Read More

                      Coerce Less And Prevent More,Do not Miss Our A-one Act on

                      More Details & Swallow In this day!

                      Cheapest Expense VigRX Plus 5 Month Supplying Male Enhancemnt Penis Enlargement Pills

                      Kindest Buy VigRX Plus 5 Month Rig out Manful Enhancemnt Penis Enlargement Pills

                      On Sellathon VigRX Additional 5 Month Supply Male Enhancemnt Penis Enlargement Pills

                      Reduce VigRX Plus 5 Month Give Virile Enhancemnt Penis Enlargement Pills

                        Write a Reply...