server = $db['server']; $this->user = $db['user']; $this->password = $db['password']; $this->name = $db['name']; $this->c = null; //$this->connect(); //$this->selectDb(); //$this->insert_id = false; //$this->affected_rows = false; $this->rows = -1; } //Member functios private function connect() { $this->c = mysql_connect($this->server, $this->user, $this->password); if ($this->c) { mysql_query('SET NAMES \'utf8\''); mysql_query('SET SESSION wait_timeout=30'); //global=60 } return $this->c or $this->error(); } private function selectDb() { $res = false; if ($this->c) { $res = mysql_select_db($this->name) or $this->error(); } return $res; } public function Close() { $res = false; if ($this->c && is_resource($this->c)) { $res = mysql_close($this->c) or $this->error(); } return $res; } public function Query($query_str) { $rs = false; if (!$this->c) { $this->c = $this->connect(); $this->selectdb(); } if ($this->c) { $rs = mysql_query($query_str) or $this->error($query_str); trigger_error($query_str, E_USER_NOTICE); if ($rs) { if (function_exists('stripos') && ( stripos($query_str, 'INSERT') !== false || stripos($query_str, 'UPDATE') !== false || stripos($query_str, 'DELETE') !== false || stripos($query_str, 'REPLACE') !== false ) ) { //$this->insert_id = mysql_insert_id($this->c); //$this->affected_rows = mysql_affected_rows($this->c); } else { $this->rows = $this->NumRows($rs); } } } return $rs; } public function InsertQuery($table, $col, $value) { if (count($col) == count($value) && count($col) > 0) { $cols = ''; $values = ''; foreach ($col as $i => $oneCol) { $cols .= ','.$oneCol; $values .= ','.$value[$i]; } $cols = substr($cols, 1); $values = substr($values, 1); $sql = 'INSERT INTO '.$table.' ('.$cols.') VALUES ('.$values.')'; return $this->Query($sql); } return false; } public function UpdateQuery($table, $col, $value, $id) { if (count($col) == count($value) && count($col) > 0 && is_numeric($id)) { $sets = ''; foreach ($col as $i => $oneCol) { $sets .= ','.$oneCol.'='.$value[$i]; } $sets = substr($sets, 1); $sql = 'UPDATE '.$table.' SET '.$sets.' WHERE id = '.$id; return $this->Query($sql); } return false; } public function DeleteQuery($table, $id) { if (is_numeric($id)) { $sql = 'DELETE FROM '.$table.' WHERE id = '.$id; return $this->Query($sql); } return false; } public function BeginTransaction() { if ($this->c) { return mysql_query('BEGIN') or $this->error(); } else { return false; } } public function Commit() { if ($this->c) { return mysql_query('COMMIT') or $this->error(); } else { return false; } } public function Rollback() { if ($this->c) { return mysql_query('ROLLBACK') or $this->error(); } else { return false; } } public function NumRows($rs) { if ($this->c) { $rows = mysql_num_rows($rs) or $this->error(); return $rows; } else { return false; } } public function FetchRow($rs) { if ($this->c) { $row = mysql_fetch_row($rs) or $this->error(); return $row; } else { return false; } } public function FetchArray($rs) { if ($this->c) { $row = mysql_fetch_array($rs) or $this->error(); return $row; } else { return false; } } public function FetchObject($rs) { if ($this->c) { $obj = mysql_fetch_object($rs) or $this->error(); return $obj; } else { return false; } } public function InsertId() { if ($this->c) { $id = mysql_insert_id() or $this->error(); return $id; } else { return false; } } public function AffectedRows() { if ($this->c) { $affectedRows = mysql_affected_rows($this->c) or $this->error(); return $affectedRows; } else { return false; } } public function SelectScalar($query_str) { $value = null; if (!$this->c) { $this->c = $this->connect(); $this->selectdb(); } if ($this->c) { $rs = $this->query($query_str); if ($this->rows > 0) { list($value) = $this->FetchRow($rs); } return $value; } return false; } public function NumFields($rs) { if ($this->c) { $fields = mysql_num_fields($rs) or $this->error(); return $fields; } return false; } public function FieldName($rs, $i) { if ($this->c) { $name = mysql_field_name($rs, $i) or $this->error(); return $name; } return false; } public function DataSeek($rs, $row=0) { if ($this->c) { $seek = mysql_data_seek($rs, $row) or $this->error(); return $seek; } return false; } public function FreeResult($rs) { if ($this->c) { $free = mysql_free_result($rs) or $this->error(); $this->insert_id = false; $this->affected_rows = false; $this->rows = -1; return $free; } return false; } public function SelectCount($query_str) { if ($this->c) { $count = 0; $rs = $this->query($query_str); if ($this->rows == 1) { list($count) = $this->FetchRow($rs); } return $count; } return false; } private function error($str="") { if (mysql_errno() == 0) { //empty result set } else { trigger_error(mysql_errno().": ".mysql_error(), E_USER_ERROR); if (IS_DEV) { echo "".mysql_errno().": ".mysql_error()."\n"; if (!empty($str)) { echo "
::$str\n"; } die("

[ Back ]"); } } } } ?>