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);
}
}