mysql數據庫連接類
/*******************************
* PHP MySQL Connect
* Author : Ray
* Date : 2005/07/30
* Updata : 2005/10/23
* Version : 0.1
*/
/* 判斷連接數據庫所需的參數是否存在 */
if (empty($dbhost))
{
$error = "<li>數據庫主機名為空</li>";
}
if (empty($dbuser))
{
$error .= "<li>數據庫用戶名為空</li>";
}
/*if (empty($dbpass))
{
$error .= "<li>數據庫密碼為空</li>";
}*/
if (empty($dbname))
{
$error .= "<li>數據庫名為空</li>";
}
if (isset($error)) showErrorMessage("1","數據庫操作失敗",$error);
/* 連接數據庫 */
if (!$connect = @mysql_connect($dbhost,$dbuser,$dbpass))
{
$error = "<li>無法連接上數據庫主機</li><li>MySQL錯誤信息:".mysql_error()."</li>";
showErrorMessage("1","數據庫連接失敗",$error);exit;
}
if (!$select_db = @mysql_select_db($dbname))
{
$error = "<li>無法打開數據庫</li><li>MySQL錯誤信息:".mysql_error()."</li>";
showErrorMessage("1","數據庫打開失敗",$error);exit;
}
mysql_query("set names 'gbk'");
/* 數據庫查詢 */
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語句在執行時遇到了一些錯誤</li><li>MySQL語句:".$mysql_query."</li><li>MySQL錯誤信息:".mysql_error()."</li>";
showErrorMessage("1","錯誤的MySql查詢",$error);exit;
}
if ($debug != 0) echo "ROWS:".mysql_num_rows($query);
return $query;
}
/* 查詢結果分解到數組 */
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;
}
}
}