欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mysql 数据库访问类

程序员文章站 2023-11-16 20:46:40
/** * @purpose: mysql数据库访问类 * @package: * @author: lisen@sellingclub.cn * @modificatio...
/**
* @purpose: mysql数据库访问类
* @package:
* @author: lisen@sellingclub.cn
* @modifications:
* @see:
* @time: 2008.10.10
*/
class db_mysql
{
//============================================================
private $host = 'localhost';
private $database = 'db_name';
private $user = 'user';
private $password = 'password';
//============================================================
private $link_id = 0; //数据库连接
private $query_id = 0; //查询结果
private $row_result = array(); //结果集组成的数组
private $field_result = array(); //结果集字段组成的数组
private $affected_rows; //影响的行数
private $rows; //结果集中记录的行数
private $fields; //结果集中字段数
private $row_postion = 0; //记录指针位置索引
public $insert_id = 0;
//************************************************************
/**** 构造函数 ****/
function __construct()
{
$this->connect();
}
/**** 析构函数 ****/
function __destruct()
{
@mysql_free_result($this->query_id);
mysql_close($this->link_id);
}
/**** 连接服务器,选择数据库 ****/
function connect($database = '',$host = '',$user = '',$password = '')
{
$database = $database == '' ? $this->database : $database;
$host = $host == '' ? $this->host : $host;
$user = $user == '' ? $this->user : $user;
$password = $password == '' ? $this->password : $password;
//-----------------------------------------------------------//
if(0 == $this->link_id)
{
$this->link_id = @mysql_pconnect($host,$user,$password);
if(!$this->link_id)
{
$this->halt('连接数据库服务端失败!');
}
if(!mysql_select_db($this->database,$this->link_id))
{
$this->halt('不能打开指定的数据库:'.$this->database);
}
}
return $this->link_id;
}
/**** 释放内存 ****/
function free()
{
if(@mysql_free_result($this->query_id))
{
unset($this->row_result);
}
$this->query_id = 0;
}
/**** 执行查询 ****/
function query($query_string)
{
//释放上次查询内存
if($this->query_id)
{
$this->free();
}
if(0 == $this->link_id)
{
$this->connect();
}
//设置中文字符集
@mysql_query('set names gb2312');
$this->query_id = mysql_query($query_string,$this->link_id);
$this->insert_id = mysql_insert_id();
if(!$this->query_id)
{
$this->halt('sql查询语句出错:'.$query_string);
}
@mysql_query('set names gb2312');
return $this->query_id;
}
/**** 将结果集指针指向指定行 ****/
function seek($pos)
{
if(@mysql_data_seek($this->query_id,$pos))
{
$this->row_position = $pos;
return true;
}
else
{
$this->halt('定位结果集发生错误!');
return false;
}
}
/**** 返回结果集组成的数组 ****/
function get_rows_array()
{
$this->get_rows();
for($i = 0; $i < $this->rows; $i++)
{
if(!mysql_data_seek($this->query_id,$i))
{
$this->halt('mysql_data_seek 查询出错!');
}
$this->row_result[$i] = mysql_fetch_array($this->query_id);
}
return $this->row_result;
}
/**** 返回结果集字段组成的数组 ****/
function get_fields_array()
{
$this->get_fields();
for($i = 0; $i < $this->fields; $i++)
{
$obj = mysql_fetch_field($this->query_id,$i);
$this->field_result[$i] = $obj->name;
}
return $this->field_result;
}
/**** 返回影响记录数 ****/
function get_affected_rows()
{
$this->affected_rows = mysql_affected_rows($this->link_id);
return $this->affected_rows;
}
/**** 返回结果集中的记录数 ****/
function get_rows()
{
$this->rows = mysql_num_rows($this->query_id);
return $this->rows;
}
/**** 返回结果集中的字段个数 ****/
function get_fields()
{
$this->fields = mysql_num_fields($this->query_id);
return $this->fields;
}
/**** 执行sql语句并返回由查询结果中第一行记录组成的数组 ****/
function fetch_one_array($sql)
{ @mysql_query('set names gb2312');
$this->query($sql);
return mysql_fetch_array($this->query_id);
}
/**** 打印错误信息 ****/
function halt($msg)
{
$this->error = mysql_error();
printf("<font style='font-family:arial,宋体;font-size:12px;'> <b>数据库发生错误:</b> %s \n",$msg);
printf("mysql 返回错误信息:</b> %s \n",$this->error);
printf("错误页面:<font style='color:#0000ee;text-decoration:underline'>%s</font> \n",$_server['php_self']);
printf(" 请将错误信息提交到系统管理员或网站程序员处理! \n");
die('<b><font color=red>脚本终止</font></b></font>');
}
}