Location: PHPKode > projects > Katropine DBmanager > dbmanager/db2mysql2pro/Mysql_Data_Handler.php
<?php
/*
 * Mysql_Data_Handler v2
 * 
 * 
 * 
 */
class Mysql_Data_Handler{
	
	private $link; // object 
	private $shownull;
	private $numrows;
	private $numfields;
	
	private $table;
	
	//error
	public $error = false; //bool
	public $errormsg;
	
	public $query_time;
	
	public function __construct($link, $table, $shownull = false){
		if(isset($link)){
			$this->link = $link;
			$this->table = $table;
			$this->shownull = $shownull;
		}else{
			die('Not a valid MySql link!');
		}
	}
	private function executeSQL($sql){
		$time_start = $this->getmicrotime();
		$result = mysql_query($sql, $this->link);
		$time_end = $this->getmicrotime();
		$this->query_time = $time_end - $time_start;
		if($result != false && $result != true){	
			$this->numrows  = mysql_num_rows($result);
			$this->numfields = mysql_num_fields($result);
		}else if ($result == true){
			$this->numrows  = mysql_affected_rows($this->link);
			$this->numfields = @mysql_num_fields($result);
		}else if($result == false){
			$this->numrows   = 0;
			$this->numfields = 0;
			$this->error = true;
			$this->errormsg = mysql_error()."<br />Error sql:<br />[ ".$sql."] <br> ";
		}
			
		return $result;
	}
	/*********************************************************************************************/
	public function __dataTable($sql){
		$result = $this->executeSQL($sql);
		$numberfields = $this->__dataNum_fields();
		if(!$this->error){
			if($numberfields != 0){
			$i=0;
			while($red = mysql_fetch_array($result)){
				for ($j=0; $j<$numberfields; $j++ ) {
					$field_name = mysql_field_name($result, $j);
					if($this->shownull){
						$red[$field_name] = (is_null($red[$field_name]))? 'NULL' : $red[$field_name];
					}
					$datapull[$i][$field_name] = $red[$field_name];
				}
			$i++;
			}
			}
		}
		return $datapull;
	}
	public function __dataScalar($sql){
		$resoult = $this->executeSQL($sql);
		$numberfields = $this->__dataNum_fields();
		if(!$this->error){
			if($numberfields != 0){	
				$redtmp = mysql_fetch_row($resoult);
				for ($j=0; $j<$numberfields; $j++ ) {
					$field_name = mysql_field_name($resoult, $j);
					if($this->shownull){
						$redtmp[$j] = (is_null($redtmp[$j]))? 'NULL' : $redtmp[$j];
					}
					$red[$field_name] = $redtmp[$j];
				}
			}
		}
		return $red[$field_name];
	}
	public function __dataRow($sql){
		$resoult = $this->executeSQL($sql);
		$numberfields = $this->__dataNum_fields();
		if(!$this->error){
			if($numberfields != 0){
				$redtmp = mysql_fetch_row($resoult);
				for ($j=0; $j<$numberfields; $j++ ) {
					$field_name = mysql_field_name($resoult, $j);
					if($this->shownull){
						$redtmp[$j] = (is_null($redtmp[$j]))? 'NULL' : $redtmp[$j];
					}
					$red[$field_name] = $redtmp[$j];
				}
			}
		}
		return $red;
	}
	
	
/*********************************************************************************************/	
	public function __insert($sql){
		
		$resoult = $this->executeSQL($sql);
		return mysql_insert_id();
		
	}
	public function __delete($sql){
		$resoult = $this->executeSQL($sql);
		return $resoult;
	}
/*
 * eg: INSERT INTO user ($key) VALUES ($param['name'], $param['password'])
 * where: $param['username'] = "Tito"
 * 			--->$key = username  {db table column title} 
 */
	public function __buildInsertQuery($param) {
		if(get_magic_quotes_gpc()) {
			foreach($param as $key=>$v){
				$param[$key] = stripslashes($param[$key]);
				
			}
		}
		foreach($param as $key=>$v){
			$param[$key] = mysql_real_escape_string($param[$key], $this->link);
			$comma = ($i == $num)? "" : ", ";  
			$fname .= $key.$comma;
			$fvalue .= "'".$param[$key]."'".$comma;
			$i++;
		}
		$sql = "INSERT INTO ".$this->table." (".$fname.") VALUES (".$fvalue.")";
		return $sql;
	}
	
/*********************************************************************************************/
	
	public function __dataNum_rows(){
		return $this->numrows;
	}
	public function __dataNum_fields(){
		return $this->numfields;
	}
	public function __mysql_get_server_info(){
		return mysql_get_server_info();
	}
	public function __mysql_info(){
		return mysql_info($this->link);
	}
	
	private function getmicrotime(){ 
		list($usec, $sec) = explode(" ",microtime()); 
		return ((float)$usec + (float)$sec); 
	} 
	
	//destructor 
	public function __destruct(){
		//echo '<h3>Destroying '.$this->table.' now...</h3>';
	}
}
?>
Return current item: Katropine DBmanager