hostname = $hostname; $this->database = $database; $this->username = $username; $this->password = $password; $this->connection = mysql_connect($this->hostname,$this->username,$this->password) or $this->choke("Can't connect to database"); if($this->database) $this->database($this->database); } function __destruct() { mysql_close($this->connection); } function database($database) { $this->database = $database; mysql_select_db($this->database,$this->connection); } function debug($email = '') { $this->debug = $email; } function dump($filename, $config = array()) { $command = DB_BIN."/mysqldump -h{$this->hostname} -u{$this->username} '-p{$this->password}' {$this->database}"; if($temp = $config['table']) $command .= " $temp"; if($temp = $config['compress']) $command .= " | $temp"; $command .= " > $filename"; $return = shell_exec($command); return $return; } function query($query,$flag = DB_DEFAULT_FLAG) { $this->last_query = $query; $resource = mysql_query($query,$this->connection) or $this->choke(); list($command,$other) = preg_split("|\s+|", $query, 2); // Load return according to query type... switch(strtolower($command)) { case("select"): case("describe"): case("desc"): case("show"): $return = array(); while($data = $this->resource_get($resource,$flag)) $return[] = $data; break; case("replace"): case("insert"): if($return = mysql_insert_id($this->connection)) $this->last_i = $return; break; default: $return = mysql_affected_rows($this->connection); } // . if($email = $this->debug) mail($email,$query,print_r($return,TRUE)); return $return; } function query_shrink($query,$flag = DB_DEFAULT_FLAG) { $return = $this->query($query); if(count($return) == 1) { $return = $return[0]; if(count($return) == 1) { $keys = array_keys($return); $return = $return[$keys[0]]; } } if(!$return) $return = NULL; return $return; } function resource_query($query,$flag = DB_DEFAULT_FLAG) { $this->last_query = $query; mysql_select_db($this->database,$this->connection); $this->last_resource = mysql_query($query,$this->connection) or $this->choke(); return $this->last_resource; } function resource_rows($resource = NULL) { if(!$resource) $resource = $this->last_resource; return mysql_num_rows($resource); } function resource_get($resource = NULL,$flag = DB_DEFAULT_FLAG) { if(!$resource) $resource = $this->last_resource; return mysql_fetch_array($resource,$flag); } function record_get($table,$id) { $query = "select * from ".$this->scrub_label($table)." where (".$this->query_where($id).")"; return $this->query_shrink($query); } function record_put($record,$table,$id = NULL) { $keys = array_keys($record); $values = array_values($record); $query_set_array = array(); for($i=0; $iscrub_label($keys[$i])."='".$this->scrub_string($values[$i])."'"; $query_set = implode(', ',$query_set_array); if($id) $query = "update ".$this->scrub_label($table)." set $query_set where (".$this->query_where($id).")"; else $query = "replace into ".$this->scrub_label($table)." set $query_set"; return $this->query($query); } function record_delete($table,$id) { $query = "delete from ".$this->scrub_label($table)." where (".$this->query_where($id).")"; return $this->query($query); } function csv($query,$filename = NULL,$config = array()) { $enclosure = ($temp = $config['enclosure']) ? $temp : '"'; $delimiter = ($temp = $config['delimiter']) ? $temp : ','; $output = ($config['output']); if($filename === NULL || $output) { $filename_original = $filename; $filename = tempnam('/tmp','csv'); if(!isset($config['output'])) $output = TRUE; } if(!($file = fopen($filename,'w'))) return FALSE; $resource = $this->resource_query($query); while($record = $this->resource_get($resource)) { if(!$rows++) fputcsv($file,array_keys($record),$delimiter,$enclosure); fputcsv($file,array_values($record),$delimiter,$enclosure); } fclose($file); if($output) { if($_SERVER['HTTP_HOST']) { header('content-type: text/csv'); header('content-length: '.filesize($filename)); if($temp = $filename_original) header("content-disposition: attachment; filename=\"$temp\""); } print(file_get_contents($filename)); unlink($filename); } return $rows; } function scrub_number($number) { if(is_numeric($number)) return($number); else { $number = preg_replace('/[^0-9\.-]/','',$number); return (is_numeric($number)) ? $number : 0; } } function scrub_string($string) { return mysql_real_escape_string($string); } function scrub_label($string) { return '`'.str_replace('.','`.`',$string).'`'; } function query_where($id) { $conditions = array(); if(is_array($id)) foreach($id as $key=>$value) $conditions[] = $this->scrub_label($key)."='".$this->scrub_string($value)."'"; else $conditions[] = $this->scrub_label(DB_DEFAULT_INDEX)."='".$this->scrub_string($id)."'"; return implode(' && ',$conditions); } function choke($error = NULL) { if(!$error) $error = mysql_error($this->connection); $this->last_error = $error; if($_SERVER['HTTP_HOST']) { print "
Database Chokes
Error: $error
Query:
{$this->last_query}
"; } else { print("Database Chokes\n"); print(" Error: $error\n"); print(" Query: {$this->last_query}\n"); } die(); } } ?>