class Db {
var $no_error = 0;
var $on_error = "report";
var $connection;
var $query_result;
var $query_count = 0;
var $query_time = 0;
var $query_array = array();
function Db($db_host, $db_user, $db_password, $db_name, $db_persistent = 0) {
if ($db_persistent) {
$this->connection = @mysql_pconnect($db_host, $db_user, $db_password)
or $this->error("Could not connect to the database server ($db_host, $db_user).");
}
else {
$this->connection = @mysql_connect($db_host, $db_user, $db_password)
or $this->error("Could not connect to the database server ($db_host, $db_user).");
}
if ($this->connection) {
if ($db_name !== "") {
$dbselect = @mysql_select_db($db_name);
if (!$dbselect) {
@mysql_close($this->connection);
$this->connection = $dbselect;
$this->error("Could not select database ($db_name).");
}
}
return $this->connection;
}
else {
return false;
}
}
function close() {
if ($this->connection) {
if ($this->query_result) {
@mysql_free_result($this->query_result);
}
$result = @mysql_close($this->connection);
return $result;
}
else {
return false;
}
}
function query($query = "") {
unset($this->query_result);
if (PRINT_STATS) {
$startsqltime = explode(" ",microtime());
}
if($query !== "") {
$this->query_result = @mysql_query($query, $this->connection)
or $this->error("Bad SQL Query: ".htmlentities($query)."
".mysql_error()."");
if (PRINT_STATS) {
$this->query_count++;
$endsqltime = explode(" ",microtime());
$this->query_time += round($endsqltime[0]-$startsqltime[0]+$endsqltime[1]-$startsqltime[1],3);
}
if (defined("PRINT_QUERIES")) {
$this->query_array[] = htmlentities($query);
}
return $this->query_result;
}
}
function query_firstrow($query = "") {
if ($query !== "") {
$this->query($query);
}
$result = $this->fetch_array($this->query_result);
$this->free_result();
return $result;
}
function fetch_array($query_id = 0, $assoc = 0) {
if(!$query_id) {
$query_id = $this->query_result;
}
if($query_id) {
if($assoc) {
return mysql_fetch_assoc($query_id);
}
else {
return mysql_fetch_array($query_id);
}
}
}
function free_result($query_id = 0) {
if(!$query_id) {
$query_id = $this->query_result;
}
if($query_id) {
mysql_free_result($query_id);
return true;
}
}
function get_numrows($query_id = 0) {
if(!$query_id) {
$query_id = $this->query_result;
}
if($query_id) {
return mysql_num_rows($query_id);
}
else {
return false;
}
}
function get_insert_id() {
if($this->connection) {
return @mysql_insert_id($this->connection);
}
else {
return false;
}
}
function get_numfields($query_id = 0) {
if(!$query_id) {
$query_id = $this->query_result;
}
if($query_id) {
return @mysql_num_fields($query_id);
}
else {
return false;
}
}
function get_fieldname($query_id = 0, $offset) {
if(!$query_id) {
$query_id = $this->query_result;
}
if($query_id) {
return @mysql_field_name($query_id, $offset);
}
else {
return false;
}
}
function affected_rows() {
if($this->connection) {
return @mysql_affected_rows($this->connection);
}
else {
return false;
}
}
function get_result($query = "") {
if ($query !== "") {
$this->query($query);
}
$i = 0;
$res = array();
while ($row = mysql_fetch_array($this->query_result)) {
$res[$i] = $row;
$i++;
}
$this->free_result();
return $res;
}
function is_empty($query = "") {
if ($query !== "") {
$this->query($query);
}
if (!mysql_num_rows($this->query_result)) {
return true;
}
else {
return false;
}
}
function not_empty($query = "") {
if ($query !== "") {
$this->query($query);
}
if (!mysql_num_rows($this->query_result)) {
return false;
}
else {
return true;
}
}
function error($errmsg) {
if(!$this->no_error) {
echo "
DB Error: ".$errmsg."
";
if ('halt' == $this->on_error) {
exit;
}
}
}
} // end of class
?>