日期:2014-05-16  浏览次数:20483 次

【转】 用PHP封装的数据库操作方法(MYSQL、ACCESS)
【转】 用PHP封装的数据库操作方法(MYSQL、ACCESS)
/**
* MYSQL操作类
* function:Open() 打开一个MYSQL连接,无返回值;
* function:Close() 关闭一个已打开的MYSQL连接,无返回值;
* function:Sql($sql) 执行一条SQL语句,
@param $sql SQL语句,
return 执行语句后的记录集;
* function:Rows() 获得记录的总条数,
return 记录的总条数;
* function:Get($field,$row) 获取指定记录行和指定字段的值,
@param $field 字段名称,
@param $row 记录行数,
return 当前记录字段的值;
* function:Next_row() 移动到下一条记录,无返回值;       
*/
class Mysql_odb
{
private $mysql_server;
private $mysql_db;
private $mysql_user;
private $mysql_pwd;
private $mysql_charset;
private $mysql_link;
private $mysql_rs;
private $mysql_rows;
/**
* MYSQL连接类构造函数
* @param $mysql_server_var - MYSQL服务器名
* @param $mysql_db_var - MYSQL数据库名
* @param $mysql_user_var - MYSQL用户名
* @param $mysql_pwd_var - MYSQL用户密码
* @param $mysql_charset_var - MYSQL连接编码方式
*/
function __construct()
{
global $mysql_server,$mysql_db,$mysql_user,$mysql_pwd,$mysql_charset;
$this->mysql_server = $mysql_server;
$this->mysql_db = $mysql_db;
$this->mysql_user = $mysql_user;
$this->mysql_pwd = $mysql_pwd;
$this->mysql_charset = $mysql_charset;
}

public function Open()
{
$this->mysql_link = mysql_connect($this->mysql_server,$this->mysql_user,$this->mysql_pwd) or die("mysql error:".mysql_error());
mysql_query("set names '".$this->mysql_charset."';",$this->mysql_link);
}

public function Close()
{
mysql_close($this->mysql_link);
}

public function Sql($sql)
{
mysql_select_db($this->mysql_db,$this->mysql_link);
$this->mysql_rs = mysql_query($sql,$this->mysql_link);
return $this->mysql_rs;
}

public function Rows()
{
$this->mysql_rows = 0;
if($this->mysql_rs != 1 && $this->mysql_rs != 0)
{
$this->mysql_rows = mysql_num_rows($this->mysql_rs);
}
return $this->mysql_rows;
}

public function Get($field,$row)
{
$result = mysql_result($this_>mysql_rs,$row,$field);
return $result;
}

public function Next_row()
{
return "NULL";
}

function __destruct()
{
unset($this->mysql_server);
unset($this->mysql_db);
unset($this->mysql_user);
unset($this->mysql_pwd);
unset($this->mysql_link);
unset($this->mysql_rs);
unset($this->mysql_charset);
unset($this->mysql_rows);
}
}

/**
* ACCESS操作类
* function:Open() 打开一个ACCESS连接,无返回值;
* function:Close() 关闭一个已打开的ACCESS连接,无返回值;
* function:Sql($sql) 执行一条SQL语句,
@param $sql SQL语句,
return 执行语句后的记录集;
* function:Rows() 获得记录的总条数,
return 记录的总条数;
* function:Get($field,$row) 获取指定记录行和指定字段的值,
@param $field 字段名称,
@param $row 记录行数,
return 当前记录字段的值;
* function:Next_row() 移动到下一条记录,无返回值;       
*/
class Access_odb
{
private $access_db;
private $access_user;
private $access_pwd;
private $access_str;
private $access_charset;
private $access_link;
private $access_rs;
private $access_rows;
/**
* ACCESS连接类构造函数
* @param $access_db_var - ACCESS数据库名
* @param $access_user_var - ACCESS用户名
* @param $access_pwd_var - ACCESS用户密码
* @param $access_charset_var - ACCESS连接编码方式
*/
function __construct()
{
global $access_db,$access_user,$access_pwd,$access_charset;
$this->access_db = $access_db;
$this->access_user = $access_user;
$this->access_pwd = $access_pwd;
$this->access_charset = $access_charset;
}

public function Open()
{
//$this->access_str =