File: /www/wwwroot//www.luckcjtw.com/include/db_class_c.php
<?php
class MyCDB
{
public $link_id = ''; #数据库连接句柄
public $result = ''; #数据库查询结果
/*构造函数初始化数据库信息 并打开数据库连接
*参数:$lHostName 数据库服务器 $lDatabase 数据库名 $lUser数据库用户名 $lPassword 数据库密码 $lPort 数据库端口
*/
public function __construct($lHostName,$lDatabase,$lUser,$lPassword,$lPort="3306")
{
#连接数据库
$this->link_id = @mysql_connect($lHostName.":".$lPort, $lUser, $lPassword);
if (!$this->link_id)
{
echo "打开数据库连接失败!<br>";
}
#选择数据库
$SelectResult = @mysql_select_db($lDatabase, $this->link_id);
if(!$SelectResult)
{
echo "选择数据库失败!<br>";
}
#设置字符集
//mysql_query('set names UTF-8',$this->link_id);
//mysql_query("SET CHARACTER SET UTF8",$this->link_id);
//mysql_query("SET CHARACTER_SET_RESULTS=UTF8'",$this->link_id);
}
#处理数据库查询 如果查询失败 返回 false 查询成功返回 true
public function query( $sql )
{
$this->result = mysql_query( $sql, $this->link_id) or die( "数据库查询失败:<br>".mysql_error()."<p>".$sql );
#写日志
global $g_write_down_sql;
if( $g_write_down_sql )
tools::Write_Log( $sql, $_SERVER['REMOTE_ADDR'].'_db_query_sql.txt' );
return true;
}
#返回当前查询结果中的记录条数
public function num_rows()
{
return mysql_num_rows($this->result);
}
/*返回查询结果中的每一行 如果没有下一行将返回 FALSE
参数 type 的值为 array 时 返回 数字为索引的数组并回字段名作为键名的数组
参数 type 的值为 row 时 返回 数字为索引的数组
参数 type 的值为 assoc 时 返回 字段名作为键名的数组
*/
public function next_row( $type = 'array' )
{
if( $type == 'array' )
return mysql_fetch_array( $this->result );
else if( $type == 'row' )
return mysql_fetch_row( $this->result );
else
return mysql_fetch_assoc( $this->result );
}
/* 此函数提供快速修改 指定数据表的字段
* $tb 要被修改的数据表名
* $set 要被修改的字段
* $where 条件
* 调用方式 update('user',"user_name='admin'","id=1")
*/
public function update( $tb,$set,$where )
{
$sql = " update $tb set $set where $where ";
//echo $sql."<p>"; EXIT();
#写日志
global $g_write_down_sql;
if( $g_write_down_sql )
tools::write_log( $sql, $_SERVER['REMOTE_ADDR'].'_db_query_sql.txt' );
mysql_query( $sql, $this->link_id) or die( "数据库查询失败:<br>".mysql_error()."<p>".$sql );
}
/* 此函数提供快速数据库的 replace 操作
* $tb 要被修改的数据表名
* $set 要被修改的字段
* 调用方式 replace('user',"user_name='admin',password='123'")
*/
public function replace( $tb,$set)
{
$sql = " replace into $tb set $set ";
//echo $sql."<p>"; EXIT();
#写日志
global $g_write_down_sql;
if( $g_write_down_sql )
tools::write_log( $sql, $_SERVER['REMOTE_ADDR'].'_db_query_sql.txt' );
mysql_query( $sql, $this->link_id) or die( "数据库查询失败:<br>".mysql_error()."<p>".$sql );
}
/* 此函数根据条件快速的从数据库中取出某个字段的值
* 根据条件如果返回多条记录那么返回第一条记录的字段值
* 参数 $tb 表名称 $field 字段名称 $where 条件
* 返回指定字段值
* 如 get_field( 'tab_user','username','userid=1')
*/
public function get_field( $tb,$field,$where)
{
$sql = " select `$field` from $tb where $where ";
#写日志
global $g_write_down_sql;
if( $g_write_down_sql )
tools::write_log( $sql, $_SERVER['REMOTE_ADDR'].'_db_query_sql.txt' );
$r = mysql_query( $sql, $this->link_id) or die( "数据库查询失败:<br>".mysql_error()."<p>".$sql );
$row = mysql_fetch_array( $r );
return $row[$field];
}
/* 此函数提供快速删除数据库记录的操作
* $tb 要被删除的数据表名
* $where 条件
* 调用方式 del('user',"id=1")
*/
public function del( $tb,$where )
{
$sql = " delete from $tb where $where ";
#写日志
global $g_write_down_sql;
if( $g_write_down_sql )
tools::write_log( $sql, $_SERVER['REMOTE_ADDR'].'_db_query_sql.txt' );
//echo $sql."<p>"; EXIT();
mysql_query( $sql, $this->link_id) or die( "数据库查询失败:<br>".mysql_error()."<p>".$sql );
}
/* 此函数返回上一步 INSERT 语句产生的 AUTO_INCREMENT 的 ID 号
* 如果上一步查询没有产生 AUTO_INCREMENT 的 ID,则返回 0。
* 要确保在insert语句之后立即调用
*/
public function get_insert_id()
{
return mysql_insert_id($this->link_id);
}
/* 此函数检查数据库中是否存在指定的表
* 参数 $tb 表示被检查的数据表名
* 如果数据库中存在 $tb 表,返回 true 否则返回 fasle
*/
public function have_table( $tb )
{
$sql = "show tables like '$tb'";
#写日志
global $g_write_down_sql;
if( $g_write_down_sql )
tools::write_log( $sql, $_SERVER['REMOTE_ADDR'].'_db_query_sql.txt' );
$r = mysql_query( $sql, $this->link_id) or die( "数据库查询失败:<br>".mysql_error()."<p>".$sql );
if( mysql_num_rows($r) > 0 )
return true;
else
return false;
}
}
?>