Mysql 數(shù)據(jù)庫訪問類
更新時間:2009年02月15日 18:27:37 作者:
Mysql數(shù)據(jù)庫訪問類 實現(xiàn)代碼,對于想學(xué)習(xí)mysql操作類的朋友值得一看
/**
* @Purpose: Mysql數(shù)據(jù)庫訪問類
* @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; //數(shù)據(jù)庫連接
private $Query_Id = 0; //查詢結(jié)果
private $Row_Result = array(); //結(jié)果集組成的數(shù)組
private $Field_Result = array(); //結(jié)果集字段組成的數(shù)組
private $Affected_Rows; //影響的行數(shù)
private $Rows; //結(jié)果集中記錄的行數(shù)
private $Fields; //結(jié)果集中字段數(shù)
private $Row_Postion = 0; //記錄指針位置索引
public $Insert_Id = 0;
//************************************************************
/**** 構(gòu)造函數(shù) ****/
function __construct()
{
$this->connect();
}
/**** 析構(gòu)函數(shù) ****/
function __destruct()
{
@mysql_free_result($this->Query_Id);
mysql_close($this->Link_Id);
}
/**** 連接服務(wù)器,選擇數(shù)據(jù)庫 ****/
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('連接數(shù)據(jù)庫服務(wù)端失敗!');
}
if(!mysql_select_db($this->Database,$this->Link_Id))
{
$this->halt('不能打開指定的數(shù)據(jù)庫:'.$this->Database);
}
}
return $this->Link_Id;
}
/**** 釋放內(nèi)存 ****/
function free()
{
if(@mysql_free_result($this->Query_Id))
{
unset($this->Row_Result);
}
$this->Query_Id = 0;
}
/**** 執(zhí)行查詢 ****/
function query($Query_String)
{
//釋放上次查詢內(nèi)存
if($this->Query_Id)
{
$this->free();
}
if(0 == $this->Link_Id)
{
$this->connect();
}
//設(shè)置中文字符集
@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;
}
/**** 將結(jié)果集指針指向指定行 ****/
function seek($pos)
{
if(@mysql_data_seek($this->Query_Id,$pos))
{
$this->Row_Position = $pos;
return true;
}
else
{
$this->halt('定位結(jié)果集發(fā)生錯誤!');
return false;
}
}
/**** 返回結(jié)果集組成的數(shù)組 ****/
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;
}
/**** 返回結(jié)果集字段組成的數(shù)組 ****/
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;
}
/**** 返回影響記錄數(shù) ****/
function get_affected_rows()
{
$this->Affected_Rows = mysql_affected_rows($this->Link_Id);
return $this->Affected_Rows;
}
/**** 返回結(jié)果集中的記錄數(shù) ****/
function get_rows()
{
$this->Rows = mysql_num_rows($this->Query_Id);
return $this->Rows;
}
/**** 返回結(jié)果集中的字段個數(shù) ****/
function get_fields()
{
$this->Fields = mysql_num_fields($this->Query_Id);
return $this->Fields;
}
/**** 執(zhí)行sql語句并返回由查詢結(jié)果中第一行記錄組成的數(shù)組 ****/
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>數(shù)據(jù)庫發(fā)生錯誤:</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(" 請將錯誤信息提交到系統(tǒng)管理員或網(wǎng)站程序員處理! \n");
die('<b><font color=red>腳本終止</font></b></font>');
}
}
* @Purpose: Mysql數(shù)據(jù)庫訪問類
* @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; //數(shù)據(jù)庫連接
private $Query_Id = 0; //查詢結(jié)果
private $Row_Result = array(); //結(jié)果集組成的數(shù)組
private $Field_Result = array(); //結(jié)果集字段組成的數(shù)組
private $Affected_Rows; //影響的行數(shù)
private $Rows; //結(jié)果集中記錄的行數(shù)
private $Fields; //結(jié)果集中字段數(shù)
private $Row_Postion = 0; //記錄指針位置索引
public $Insert_Id = 0;
//************************************************************
/**** 構(gòu)造函數(shù) ****/
function __construct()
{
$this->connect();
}
/**** 析構(gòu)函數(shù) ****/
function __destruct()
{
@mysql_free_result($this->Query_Id);
mysql_close($this->Link_Id);
}
/**** 連接服務(wù)器,選擇數(shù)據(jù)庫 ****/
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('連接數(shù)據(jù)庫服務(wù)端失敗!');
}
if(!mysql_select_db($this->Database,$this->Link_Id))
{
$this->halt('不能打開指定的數(shù)據(jù)庫:'.$this->Database);
}
}
return $this->Link_Id;
}
/**** 釋放內(nèi)存 ****/
function free()
{
if(@mysql_free_result($this->Query_Id))
{
unset($this->Row_Result);
}
$this->Query_Id = 0;
}
/**** 執(zhí)行查詢 ****/
function query($Query_String)
{
//釋放上次查詢內(nèi)存
if($this->Query_Id)
{
$this->free();
}
if(0 == $this->Link_Id)
{
$this->connect();
}
//設(shè)置中文字符集
@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;
}
/**** 將結(jié)果集指針指向指定行 ****/
function seek($pos)
{
if(@mysql_data_seek($this->Query_Id,$pos))
{
$this->Row_Position = $pos;
return true;
}
else
{
$this->halt('定位結(jié)果集發(fā)生錯誤!');
return false;
}
}
/**** 返回結(jié)果集組成的數(shù)組 ****/
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;
}
/**** 返回結(jié)果集字段組成的數(shù)組 ****/
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;
}
/**** 返回影響記錄數(shù) ****/
function get_affected_rows()
{
$this->Affected_Rows = mysql_affected_rows($this->Link_Id);
return $this->Affected_Rows;
}
/**** 返回結(jié)果集中的記錄數(shù) ****/
function get_rows()
{
$this->Rows = mysql_num_rows($this->Query_Id);
return $this->Rows;
}
/**** 返回結(jié)果集中的字段個數(shù) ****/
function get_fields()
{
$this->Fields = mysql_num_fields($this->Query_Id);
return $this->Fields;
}
/**** 執(zhí)行sql語句并返回由查詢結(jié)果中第一行記錄組成的數(shù)組 ****/
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>數(shù)據(jù)庫發(fā)生錯誤:</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(" 請將錯誤信息提交到系統(tǒng)管理員或網(wǎng)站程序員處理! \n");
die('<b><font color=red>腳本終止</font></b></font>');
}
}
您可能感興趣的文章:
- ASP.NET封裝的SQL數(shù)據(jù)庫訪問類
- linux mysql 數(shù)據(jù)庫開啟外部訪問設(shè)置指南
- C#訪問PostGreSQL數(shù)據(jù)庫的方法
- SQLServer 數(shù)據(jù)庫變成單個用戶后無法訪問問題的解決方法
- MySQL數(shù)據(jù)庫設(shè)置遠(yuǎn)程訪問權(quán)限方法小結(jié)
- mysql設(shè)置遠(yuǎn)程訪問數(shù)據(jù)庫的多種方法
- Python 分析Nginx訪問日志并保存到MySQL數(shù)據(jù)庫實例
- c#編寫的高并發(fā)數(shù)據(jù)庫控制訪問代碼
- ASP.NET對SQLServer的通用數(shù)據(jù)庫訪問類
- ADO.NET通用數(shù)據(jù)庫訪問類
相關(guān)文章
linux CentOS 7.4下 mysql5.7.20 密碼改回來的處理方法
這篇文章主要介紹了linux CentOS 7.4下 mysql5.7.20 密碼改回來的處理方法,需要的朋友可以參考下2018-11-11解析在MYSQL語法中使用trim函數(shù)刪除兩側(cè)字符
本篇文章是對在MYSQL語法中使用trim函數(shù)刪除兩側(cè)字符的實現(xiàn)方法進行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-073步搞定純真IP數(shù)據(jù)導(dǎo)入到MySQL的方法詳解
免編程,3步搞定純真IP數(shù)據(jù)導(dǎo)入到MySQL詳解,好多做ip地址查詢的朋友用的到。2009-10-10Ubuntu下取消MySQL數(shù)據(jù)庫本機綁定限制方法
在Ubuntu系統(tǒng)中,添加了MySQL賬戶,賦予了數(shù)據(jù)庫完全操作權(quán)限,并且允許數(shù)據(jù)庫從外部鏈接 但是,還是無法遠(yuǎn)程訪問MySQL數(shù)據(jù)庫2013-06-06詳解MySQL 重做日志(redo log)與回滾日志(undo logo)
這篇文章主要介紹了MySQL redo與undo日志的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)MySQL,感興趣的朋友可以了解下2020-08-08MySQL8.0?索引優(yōu)化invisible?index詳情
這篇文章主要介紹了MySQL8.0?索引優(yōu)化invisible?index詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Mysql快速插入千萬條數(shù)據(jù)的實戰(zhàn)教程
這篇文章主要給大家介紹了關(guān)于Mysql快速插入千萬條數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03mysql、oracle默認(rèn)事務(wù)隔離級別的說明
這篇文章主要介紹了mysql、oracle默認(rèn)事務(wù)隔離級別的說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01