host = $h; $this->base = $b; $this->user = $u; $this->pass = $p; $this->connect(); $this->setchars($ch); } //------------------------------------------------------------------------------------------------------------------------------------- function connect() { $db_connect = @mysql_connect($this->host, $this->user, $this->pass); $db_select = @mysql_select_db($this->base); if (!$db_connect) { die("
Unable to establish connection to MySQL
".mysql_errno()." : ".mysql_error()."
"); } elseif (!$db_select) { die("
Unable to select MySQL database
".mysql_errno()." : ".mysql_error()."
"); } } //------------------------------------------------------------------------------------------------------------------------------------- function setchars($ch="utf8"){ @mysql_query("SET NAMES $ch") or die("
MySQL database error: NAMES.
"); @mysql_query("SET CHARACTER SET $ch") or die("
MySQL database error: CHARACTER .
"); @mysql_query("SET CHARACTER_SET_SERVER = $ch") or die("
MySQL database error: CHARACTER_SET_SERVER.
"); @mysql_query("SET CHARACTER_SET_CONNECTION = $ch") or die("
MySQL database error: CHARACTER_SET_CONNECTION.
"); @mysql_query("SET CHARACTER_SET_DATABASE = $ch") or die("
MySQL database error: CHARACTER_SET_DATABASE.
"); } //------------------------------------------------------------------------------------------------------------------------------------- function update($table, $data, $where=""){ $query = "UPDATE ".$table." SET "; $datastr = ""; $i=0; foreach ($data as $key=>$item){ $datastr.=(($i)?", ":""); $datastr.=$key."="; $datastr.="'".$item."'"; $i++; } $query.=$datastr; if ($where) $query.=" WHERE ".$where; $this->query($query); } //------------------------------------------------------------------------------------------------------------------------------------- function add($table, $data){ $datastr = "("; $valuestr = "("; $i=0; foreach ($data as $key=>$item){ if ($i){ $datastr.=", "; $valuestr.=", "; } $datastr.=$key; $valuestr.="'".$item."'"; $i++; } $datastr.=")"; $valuestr.=")"; $query = "INSERT INTO ".$table." ".$datastr." VALUES ".$valuestr; $this->query($query); } //------------------------------------------------------------------------------------------------------------------------------------- function delete($table, $where=""){ $query = "DELETE FROM ".$table; if ($where){ $query.=" WHERE ".$where; } $this->query($query); } //------------------------------------------------------------------------------------------------------------------------------------- function dbcount($field, $table, $conditions = "") { $cond = ($conditions ? " WHERE ".$conditions : ""); $result = @mysql_query("SELECT Count".$field." FROM ".$table.$cond); if (!$result) { $this->LOG(mysql_error()); return false; } else { $rows = mysql_result($result, 0); return $rows; } } //------------------------------------------------------------------------------------------------------------------------------------- function dbarray($query) { $result = @mysql_fetch_assoc($query); if (!$result) { $this->LOG(mysql_error()); return false; } else { return $result; } } //------------------------------------------------------------------------------------------------------------------------------------- function dbarraynum($query) { $result = @mysql_fetch_row($query); if (!$result) { $this->LOG(mysql_error()); return false; } else { return $result; } } //------------------------------------------------------------------------------------------------------------------------------------- function dbrows($query) { $result = @mysql_num_rows($query); return $result; } //------------------------------------------------------------------------------------------------------------------------------------- function query($str){ $result = @mysql_query($str); if (!$result) { $this->LOG(mysql_error()); return false; } else { return $result; } } //------------------------------------------------------------------------------------------------------------------------------------- function LOG($err){ echo "My error: $err"; } //------------------------------------------------------------------------------------------------------------------------------------- } ?>