db=array('name'=>$db_name,'user'=>$user,'pass'=>$pass,'host'=>$host); } function record_error() { $this->failed=TRUE; if($this->debug) { $this->debugtext[]="ERROR: ".mysql_error(); if($this->query) $this->query="FAILED: ".$query; } } function connect($persistent=false) { $this->failed=FALSE; $this->db['pers']=$persistent; if($persistent) $this->link_id=mysql_pconnect($this->db['host'],$this->db['user'],$this->db['pass']); else $this->link_id=mysql_connect($this->db['host'],$this->db['user'],$this->db['pass']); mysql_select_db($this->db['name']) or $this->record_error(); //if($this->debug) //{ list($usec, $sec) = explode(" ", microtime()); // measure connection time ( or script time if DB was linked at the beginning of the script $this->link_time=(float)$usec+(float)$sec; //} return !$this->failed; // false if failed connection } function disconnect() { $this->freeResult(); if($this->db['pers']) mysql_close($this->link_id); if($this->debug) { list($usec, $sec) = explode(" ", microtime()); $this->link_time=(float)$usec+(float)$sec-$this->link_time; $this->debugtext[]="END Connection: ".(string)$this->link_time." sec"; } } function query($query) { $this->freeResult(); $this->sqlr=mysql_query($query) or $this->record_error(); //if($this->debug) // debug mode ( records queries ) $this->debugtext[]="$query"; return $this->sqlr; } function getRow() { if(is_resource($this->sqlr)) return mysql_fetch_assoc($this->sqlr); return FALSE; } function freeResult() { if(isset($this->sqlr)&&is_resource($this->sqlr)) { mysql_free_result($this->sqlr); unset($this->sqlr); } } function getLine($query,$id=FALSE) { if($id===FALSE) $this->query($query); else $this->query($query." WHERE `id`='$id'"); if(is_resource($this->sqlr)) { $line=mysql_fetch_assoc($this->sqlr); $this->freeResult(); return $line; } return FALSE; } function getTable($query,$useid=FALSE) { $table=array(); if($this->query($query)==FALSE) return FALSE; while($line=mysql_fetch_assoc($this->sqlr)) { if($useid) $table[$line['id']]=$line; else $table[]=$line; } return $table; } function insertRow($table,$array) { if(count($array)==0) return; $comma='';$listofvalues=''; $listofelements=''; foreach($array as $elem=>$value) { if($value===NULL) $listofvalues.="{$comma}NULL"; else { $value=mysql_escape_string($value); $listofvalues.="$comma'$value'"; } $listofelements.="$comma`$elem`"; $comma=','; }; $query = "INSERT HIGH_PRIORITY INTO $table (".$listofelements.") VALUES(".$listofvalues.")"; $this->query("$query"); } function updateRow($table,$array,$condition) { if(count($array)==0) return; $q="UPDATE $table SET "; foreach($array as $index=>$value) { if($value==NULL) $q.="`$index`=NULL, "; else { $value=mysql_escape_string($value); $q.="`$index`='$value', "; } } $q=substr($q,0,-2)." WHERE $condition LIMIT 1"; $this->query($q); } function deleteRow($table,$condition,$single=true) { $q="DELETE FROM $table WHERE $condition".($single?" LIMIT 1":""); $this->query($q); } function getInfo_tableStatus($tablename,$what_to_get='all') { $line=$this->getLine("SHOW TABLE STATUS LIKE '$tablename'"); switch($what_to_get) { case 'all': return array('rows'=>$line['Rows'],'autoinc'=>$line['Auto_increment']); case 'rows': return $line['Rows']; case 'autoinc': return $line['Auto_increment']; } } function getInfo_countRows($tablename,$condition=false) { if($condition) $line=$this->getLine("SELECT COUNT(*) FROM `$tablename` WHERE $condition"); else $line=$this->getLine("SELECT COUNT(*) FROM `$tablename`"); return $line['COUNT(*)']; } } //----------------------------------------------------------------- // Start Interface //----------------------------------------------------------------- @$db=new wamis_DB("$database_name","$database_username","$database_password","$database_server"); function DB_initialize($debug=false) { global $db; $db->debug=$debug; $db->connect(); } function DB_shutdown() { global $db; $db->disconnect(); if($db->debug) { ?>
debugtext as $idx=>$row) echo ""; ?>
DB Debug
#Query
$idx$row