database = 'adsystem'; $db->user = 'sa'; $db->password = 'sa'; $db->server = 'localhost'; // Connect to db $db->connect(); ////////////////////////////////////////////////////////////////////// function ss($str) { return mysql_escape_string($str); } // Mysql Database Class class DB_MySQL { var $database = ""; var $link_id = 0; var $query_id = 0; var $record = array(); var $server = ""; var $user = ""; var $password = ""; function connect() { global $usepconnect; // connect to db server if ( 0 == $this->link_id ) { if ($this->password=="") { if ($usepconnect==1) { $this->link_id=mysql_pconnect($this->server,$this->user); } else { $this->link_id=mysql_connect($this->server,$this->user); } } else { if ($usepconnect==1) { $this->link_id=mysql_pconnect($this->server,$this->user,$this->password); } else { $this->link_id=mysql_connect($this->server,$this->user,$this->password); } } if (!$this->link_id) { $this->halt("Link-ID == false, connect failed"); } if ($this->database!="") { if(!mysql_select_db($this->database, $this->link_id)) { $this->halt("cannot use database ".$this->database); } } } } function geterrdesc() { $this->error=mysql_error(); return $this->error; } function geterrno() { $this->errno=mysql_errno(); return $this->errno; } function select_db($database="") { // select database if ($database!="") { $this->database=$database; } if(!mysql_select_db($this->database, $this->link_id)) { $this->halt("cannot use database ".$this->database); } } function query($query_string) { global $query_count,$showqueries,$explain,$querytime; // do query if ($showqueries) { echo "Query: $query_string\n"; global $pagestarttime; $pageendtime=microtime(); $starttime=explode(" ",$pagestarttime); $endtime=explode(" ",$pageendtime); $beforetime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1]; echo "Time before: $beforetime\n"; } $this->query_id = mysql_query($query_string,$this->link_id); if (!$this->query_id) { $this->halt("Invalid SQL: ".$query_string); } $query_count++; if ($showqueries) { $pageendtime=microtime(); $starttime=explode(" ",$pagestarttime); $endtime=explode(" ",$pageendtime); $aftertime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1]; $querytime+=$aftertime-$beforetime; echo "Time after: $aftertime\n"; if ($explain and substr(trim(strtoupper($query_string)),0,6)=="SELECT") { $explain_id = mysql_query("EXPLAIN $query_string",$this->link_id); echo "\n"; echo "
| table | type | possible_keys | key | key_len | ref | rows | Extra |
| $array[table] | $array[type] | $array[possible_keys] | $array[key] | $array[key_len] | $array[ref] | $array[rows] | $array[Extra] |
";
} else {
echo "\n
\n\n";
}
}
return $this->query_id;
}
function fetch_array($query_id=-1,$query_string="") {
// retrieve row
if ($query_id!=-1) {
$this->query_id=$query_id;
}
if ( isset($this->query_id) ) {
$this->record = mysql_fetch_array($this->query_id);
} else {
if ( !empty($query_string) ) {
$this->halt("Invalid query id (".$this->query_id.") on this query: $query_string");
} else {
$this->halt("Invalid query id ".$this->query_id." specified");
}
}
return $this->record;
}
function free_result($query_id=-1) {
// retrieve row
if ($query_id!=-1) {
$this->query_id=$query_id;
}
return @mysql_free_result($this->query_id);
}
function query_first($query_string) {
// does a query and returns first row
$query_id = $this->query($query_string);
$returnarray=$this->fetch_array($query_id, $query_string);
$this->free_result($query_id);
return $returnarray;
}
function data_seek($pos,$query_id=-1) {
// goes to row $pos
if ($query_id!=-1) {
$this->query_id=$query_id;
}
return mysql_data_seek($this->query_id, $pos);
}
function num_rows($query_id=-1) {
// returns number of rows in query
if ($query_id!=-1) {
$this->query_id=$query_id;
}
return mysql_num_rows($this->query_id);
}
function num_fields($query_id=-1) {
// returns number of fields in query
if ($query_id!=-1) {
$this->query_id=$query_id;
}
return mysql_num_fields($this->query_id);
}
function sql_query($query) {
$result = $this->query($query);
$return_array = array();
while ($row = $this->fetch_array($result)) {
array_push($return_array, $row);
}
$this->free_result($result);
return $return_array;
}
function halt($msg) { die($msg); }
function insert_id() { return mysql_insert_id($this->link_id); }
function close() { return mysql_close(); }
}
?>