mysql数据库连接类
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;
}
}
}
补充:数据库,mysql教程