Location: PHPKode > scripts > tc_mydb > tc_mydb/cls.tc_mydb.php
<?php
/**
 * DB User name
 *
 */
	define("DB_USER", "your_username");		
/**
 * DB Password
 *
 */
	define("DB_PASSWORD", "your_password");					
/**
 * DB Name
 *
 */	
	define("DB_NAME", "your_database");		
/**
 * DB Host
 *
 */
	define("DB_HOST", "localhost");	

/**
 * MAINDB Version
 *
 */
	define("MAINMYSQL_VERSION","0.1.0");
/**
 * DEFINE OBJECT RETURN
 *
 */
	define("OBJ","OBJ",true);
/**
 * DEFINE ASC ARRAY RETURN
 *
 */
	define("A_A","A_A",true);
/**
 * DEFINE NUM ARRAY RETURN
 *
 */
	define("A_N","A_N",true);
/**
 * ErrorHead
 *
 */
	define("ERROR_REPORT_HEAD","<div class=\"errorRow\"><span class=\"errorHdl\">DATENBANK FEHLER</span>\n");
/**
 * ErrorRow
 *
 */
	define("ERROR_REPORT_ROW","<span class=\"errorNo\">Nummer:<br />{ERRORNO}</span><span class=\"errorMsg\">{ERRORSTRING}</span></div>\n");
/**
 * Datenbankklasse
 * @author 		Oliver Müller <hide@address.com>
 * @copyright 	Oliver Müller, Kalksteinweg 20, 12349 Berlin
 * @version 	0.1.0
 * @return 		Object
 * 
 */
class tc_mydb implements i_db  
{	
	/**
	 * Databaseobject
	 *
	 * @var object
	 */
	protected $dbo;
	/**
	 * Errorstring
	 *
	 * @var string
	 */
	protected $errorStr;
	/**
	 * Error HTML - String
	 *
	 * @var string
	 */
	protected $errorHTML;
	/**
	 * Mysqli ErrorNo
	 *
	 * @var Integer
	 */
	protected $errorno	= 	false;
	/**
	 * Printerrorswitcher
	 *
	 * @var boolean
	 */
	protected $printError	=	true;
	/**
	 * Resultcache
	 * 
	 * @var string
	 */
	protected $lastResult;
	/**
	 * Querycache
	 *
	 * @var string
	 */
	protected $result			= 	null;
	protected $function_call	=	'';
	protected $num_queries		=	0;
	protected $num_rows			=	0;
	protected $lastQuery		=	'';
	protected $rows_affected	=	0;
	/**
	 * InsertIDcache
	 *
	 * @var integer
	 */
	protected $lastID;
	/**
	 * ColInfocache
	 *
	 * @var string
	 */
	protected $colInfo;
	
	protected $log	= false;
	
	/**
	 * Constructor of Class
	 *
	 */
	function __construct() 
	{
		$this->dbo = @new mysqli(DB_HOST,DB_USER,DB_PASSWORD);
		if (mysqli_connect_errno()){
			$this->errorStr=mysqli_connect_error();$this->errorno=mysqli_connect_errno();
			$this->_renderError();
		}else{
			$this->selectDB(DB_NAME);	
		}
		
	}
	
	public function Log_start()
	{
		$this->log = true;
	}
	
	public function Log_ended()
	{
		$this->log = false;
	}
	
	/**
	 * Select Database with $this->dbo
	 *
	 * @param string $db_name 
	 * @return boolean
	 */
	public function selectDB($db_name)
	{
		$this->dbo->select_db($db_name);
		if (mysqli_errno($this->dbo)) {
			$this->errorStr=mysqli_error($this->dbo);$this->errorno=mysqli_errno($this->dbo);
			$this->_renderError();
			return false;
		}else{
			return true;
		}
	}
	/**
	 * Render Error to HTML
	 *
	 * @return boolean
	 */
	private function _renderError()
	{
		if ($this->printError && $this->errorno) {
			$this->errorHTML = ERROR_REPORT_HEAD;
			$this->errorHTML .= str_replace("{ERRORNO}",$this->errorno,ERROR_REPORT_ROW);
			$this->errorHTML = str_replace("{ERRORSTRING}",$this->errorStr,$this->errorHTML);
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * printError Methode
	 * if Error return HTML - String
	 * else return false;
	 * @return mixed Return string if Errorno && printError else return false
	 */
	public function print_Error()
	{
		if ($this->printError && $this->errorno) {
			return $this->errorHTML;
		}else{
			return false;
		}
	}
	
	/**
	 * Methode set printError to True
	 *
	 */
	public function show_errors()
	{
		$this->printError = true;
	}
	
	/**
	 * Methode set printError to False
	 *
	 */
	public function hide_errors()
	{
		$this->printError = false;
	}
	
	
	/**
	 * Methode return Escapestring for Mysqli
	 *
	 * @param string $string
	 * @return string
	 */
	protected function _escape($string)
	{
//		return mysqli_escape_string($this->dbo,$string);
		return $string;
	}
	/**
	 * Methode to kill resultcache
	 *
	 */
	private function _flush()
	{
		$this->colInfo=null;
		$this->lastQuery=null;
		$this->lastID=null;
		$this->lastResult=null;
	}
	
	public function query($query)
	{
		$query = trim($this->_escape($query));
		
		if ($this->log) {
		 	require_once(FULL_CLASSES."class.file.php");
		 	$logfile = new file();
		 	$log_content = "<sql>\n\t<date>".date("d.m.Y H:i:s",time())."</date>\n\t\t<string>".$query.";\n\t\t</string>\n</sql>\n";
		 	$logfile->Write_to_File(FULL_LOGS.date("Y_m_d_",time()).LOGFILE,$log_content,"a+");
		} 
		
		$return_val = 0;
		$this->_flush();
		$this->function_call = "\$object->query(\"$query\")";
		$this->lastQuery = $query;
		$this->result = @mysqli_query($this->dbo,$query);
		$this->num_queries++;
		if ( mysqli_errno($this->dbo))
		{
			$this->errorStr=mysqli_error($this->dbo);$this->errorno=mysqli_errno($this->dbo);
			$this->_renderError();
			return false;
		}
		
		if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
		{
			$this->rows_affected = mysqli_affected_rows($this->dbo);
			if ( preg_match("/^(insert|replace)\s+/i",$query) )
			{
				$this->lastID = mysqli_insert_id($this->dbo);	
			}
			$return = $this->rows_affected;
		}
		else
		{
			$i=0;
			while ($i < @mysqli_num_fields($this->result))
			{
				$this->colInfo[$i] = @mysqli_fetch_field($this->result);
				$i++;
			}
			$num_rows=0;
			while ( $row = @mysqli_fetch_object($this->result) )
			{
				$this->lastResult[$num_rows] = $row;
				$num_rows++;
			}
			@mysqli_free_result($this->result);
			$this->num_rows = $num_rows;
			$return = $this->num_rows;
		}
//		$this->trace || $this->debug_all ? $this->debug() : null ;
		return $return;
	}
	
	public function get_db_var($query=null,$x=0,$y=0)
	{
		$this->function_call = "\$object->get_var(\"$query\",$x,$y)";
		if ( $query )
		{
			$this->query($query);
		}
		if ( $this->lastResult[$y] )
		{
			$values = array_values(get_object_vars($this->lastResult[$y]));
		}
		return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
	}
	
	public function get_row($query=null,$output=A_A,$y=0)
	{
		$this->function_call = "\$object->get_row(\"$query\",$output,$y)";
		if ( $query )
		{
			$this->query($query);
		}
		if ( $output == OBJ )
		{
			return $this->lastResult[$y]?$this->lastResult[$y]:null;
		}
		elseif ( $output == A_A )
		{
			return $this->lastResult[$y]?get_object_vars($this->lastResult[$y]):null;
		}
		elseif ( $output == A_N )
		{
			return $this->lastResult[$y]?array_values(get_object_vars($this->lastResult[$y])):null;
		}
		else
		{
			$this->errorno=90001;
			$this->errorStr=" \$object->get_row(string query, output type, int offset) -- Output type must be one of: OBJ, A_A, A_N";
			$this->_renderError();
		}
	}
	
	public function get_col($query=null,$x=0)
	{
		if ( $query )
		{
			$this->query($query);
		}
		for ( $i=0; $i < count($this->lastResult); $i++ )
		{
			$new_array[$i] = $this->get_db_var(null,$x,$i);
		}
		return $new_array;
	}
	
	public function get_results($query=null, $output = A_A)
	{
		$this->function_call = "\$object->get_results(\"$query\", $output)";
		if ( $query )
		{
			$this->query($query);
		}
		if ( $output == OBJ )
		{
			return $this->lastResult;
		}
		elseif ( $output == A_A || $output == A_N )
		{
			if ( $this->lastResult )
			{
				$i=0;
				foreach( $this->lastResult as $row )
				{

					$new_array[$i] = get_object_vars($row);

					if ( $output == A_N )
					{
						$new_array[$i] = array_values($new_array[$i]);
					}

					$i++;
				}

				return $new_array;
			}
			else
			{
				return null;
			}
		}
	}
	
	protected function get_col_info($info_type="name",$col_offset=-1)
	{
		if ( $this->colInfo )
		{
			if ( $col_offset == -1 )
			{
				$i=0;
				foreach($this->colInfo as $col )
				{
					$new_array[$i] = $col->{$info_type};
					$i++;
				}
				return $new_array;
			}
			else
			{
				return $this->colInfo[$col_offset]->{$info_type};
			}

		}
	}
	
	public function Get_InsertID()
	{
		return $this->lastID;
	}
	
	function __destruct()
	{
		 @mysqli_close($this->dbo);
	}
}
class stmt_db extends db  
{
	function __construct(){
		parent::__construct();
	}
	
	function __destruct(){
		parent::__destruct();
	}
}
interface i_db
{
	function Log_start();
	function Log_ended();
	function Get_InsertID();	
	function show_errors();	
	function hide_errors();
	function print_Error();
	function selectDB($db_name);
	function query($query);
	function get_row($query=null,$output=A_A,$y=0);
	function get_col($query=null,$x=0);
	function get_results($query=null, $output=A_A);
	function get_db_var($query=null,$x=0,$y=0);
}
?>
Return current item: tc_mydb