m = $_m; $this->pre = $_m ['pre']; $this->salt = $_m ['salt']; if (SQL_LOG) $this->f = fopen ( 'sql.log', 'w' ); $this->h = mysql_connect ( $_m ['host'], $_m ['user'], $_m ['pass'] ); if ($this->h) { return false; } mysql_select_db ( $_m ['data'] ); return true; } private function prepare($q, $t, $p) { $t = str_split ( $t ); $v = array (); $v [] = str_replace ( "?", "%s", $q ); foreach ( $t as $i => $c ) { switch ($c) { case 's' : $v [] = "'" . mysql_real_escape_string ( $c ) . "'"; break; case 'i' : $v [] = ( int ) $c; break; case 'd' : $v [] = ( double ) $c; break; default : return false; } $res = call_user_func_array ( 'mysql_query', $v ); if (! $res) { return false; } return $res; } } public function get($que, $t = '', $p = array ()) { // echo $que; $this->cnt_get ++; if (SQL_LOG) fputs ( $this->f, str_replace ( array ( "\n", " " ), array ( ' ', '' ), $que ) . "\n" . print_r ( $p, true ) . "\n\n" ); $res = $this->prepare ( $que, $t, $p ); $ret = array (); // print_r($statement); if (! $res) { if (SQL_LOG) { fputs ( $this->f, mysql_error () ); return false; } } while ( $row = mysql_fetch_assoc ( $res ) ) { $ret [] = $row; } return $ret; } public function single($que, $t = '', $p = array ()) { $data = $this->get ( $que, $t, $p ); if ($data) { return $data [0]; } return false; } public function list($que, $t = '', $p = array ()) { $data = $this->get ( $que, $t, $p ); if ($data) { $ret = array (); foreach ( $data as $d ) { foreach ( $d as $k => $v ) { $ret [] = $v; } } return $ret; } return false; } public function keyval($que, $k, $v, $t = '', $p = array ()) { $data = $this->get ( $que, $t, $p ); if ($data) { $ret = array (); foreach ( $data as $d ) { $ret [$d [$k]] = $d [$v]; } return $ret; } return false; } public function set($que, $t = '', $p = array (), $id = false) { // echo $que; $this->cnt_set ++; if (SQL_LOG) fputs ( $this->f, str_replace ( array ( "\n", " " ), array ( ' ', '' ), $que ) . "\n" . print_r ( $p, true ) . "\n\n" ); $res = $this->prepare ( $que, $t, $p ); if (! $res) { if (SQL_LOG) fputs ( $this->f, mysql_error () ); return false; } if ($id) { return mysql_insert_id ( $res ); } else { return ( bool ) mysql_affected_rows ( $res ); } } function __destruct() { mysql_close ( $this->h ); } } ?>