MySQL Database connection class
/*******************************
* PHP MySQL Connect
* Author:ray
* DATE:2005/07/30
* UPDATA:2005/10/23
* version:0.1
*/
/* Determine if the parameters required to connect to the database exist.
if (empty ($dbhost))
{
$error = "<li> database host name is null </li>";
}
if (empty ($dbuser))
{
$error. = "<li> database user name is null </li>";
}
/*if (Empty ($dbpass))
{
$error. = "<li> database password is null </li>";
}*/
if (empty ($dbname))
{
$error. = "<li> database name is null </li>";
}
if (Isset ($error)) showerrormessage ("1", "Database operation failed", $error);
/* Connection Database * *
if (! $connect = @mysql_connect ($dbhost, $dbuser, $dbpass))
{
$error = "<li> cannot connect to database host </li><li>mysql error message:". Mysql_error (). " </li> ";
Showerrormessage ("1", "Database connection Failed", $error);
}
if (! $select _db = @mysql_select_db ($dbname))
{
$error = "<li> Cannot open database </li><li>mysql error message:". Mysql_error (). </li> ";
Showerrormessage ("1", "Database open Failed", $error);
}
mysql_query ("Set names ' GBK '");
/* Database Query * *
function Dbquery ($mysql _query, $debug = 0)
{
if ($debug!= 0)
{
echo $mysql _query. " n ";
if ($debug = = 1) return;
}
$GLOBALS [' MySQL '] [' query_count ']++;
if (! $query = @mysql_query ($mysql _query))
{
$error = "<li>mysql statement encountered some error </li><li>mysql statement at execution:". $mysql _query. " </li><li>mysql error message: ". Mysql_error ()." </li> ";
Showerrormessage ("1", "wrong MySQL query", $error); exit;
}
if ($debug!= 0) echo "ROWS:". mysql_num_rows ($query);
return $query;
}
/* Query results are decomposed into arrays * *
function Dbarray ($mysql _result)
{
Return @mysql_fetch_array ($mysql _result);
}
function Querybot ($query _data)
{
if (!empty ($query _data[' field '))
{
foreach ($query _data[' field '] as $field => $value)
{
Switch ($query _data[' action ')]
{
Case ' Insert ':
$query _insert_field[(int) $i]. = "'. $field." `";
$query _insert_value[(int) $i]. = "'" $value. "'";
Break
Case ' Update ':
$query _update_field[(int) $i]. = "'. $field." ' = ' '. $value. "'";
unset ($query _update_where);
foreach ($query _data[' where '] as $field => $value)
{
$query _update_where[(int) $j]. = "'. $field." ' = ' '. $value. "'";
}
$j + +;
Break
}
$i + +;
}
Switch ($query _data[' action ')]
{
Case ' Insert ':
Dbquery ("INSERT into". $GLOBALS [' prefix ']. $query _data[' table ']. " ' (". Implode (", ", $query _insert_field).") VALUES (". Implode (", ", $query _insert_value).");
Break
Case ' Update ':
Dbquery ("UPDATE") $GLOBALS [' prefix ']. $query _data[' table '. " ' SET '. Implode (",", $query _update_field). " WHERE ". Implode (" and ", $query _update_where).
Break
}
}
}