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 ($query != "") {
list($usec, $sec) = explode(' ',microtime());
$querytime_before = ((float)$usec + (float)$sec);
$this->query_result = @mysql_query($query, $this->connection);
list($usec, $sec) = explode(' ',microtime());
$querytime_after = ((float)$usec + (float)$sec);
$this->query_time = round($querytime_after - $querytime_before,3);
return $this->query_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 query_firstrow($query = "") {
if ($query != "") {
$this->query($query);
}
$result = $this->fetch_array($this->query_result);
$this->free_result();
return $result;
}
function 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 tbl_numrows($table) {
if ($table) {
$query_id = $this->query("SELECT * FROM ".$table);
}
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 get_fieldtype($query_id = 0, $offset) {
if (!$query_id) {
$query_id = $this->query_result;
}
if ($query_id) {
return @mysql_field_type($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 report(){
return mysql_error();
}
function error($errmsg) {
if (!$this->no_error) {
echo "
DB Error: ".$errmsg."
";
if ('halt' == $this->on_error) {
exit;
}
}
}
} // end of class
?>