Mysql數(shù)據(jù)庫(kù)操作類( 1127版,提供源碼下載 )
更新時(shí)間:2010年12月02日 22:04:55 作者:
Mysql數(shù)據(jù)庫(kù)操作類,學(xué)習(xí)php的朋友可以參考下。
Mysql.class.php 下載
<?php
class Mysql {
private $db_host; //主機(jī)地址
private $db_user; //用戶名
private $db_pass; //連接密碼
private $db_name; //名稱
private $db_charset; //編碼
private $conn;
public $debug=false;//調(diào)試開(kāi)關(guān),默認(rèn)關(guān)閉
private $query_id; //用于判斷sql語(yǔ)句是否執(zhí)行成功
private $result; //結(jié)果集
private $num_rows; //結(jié)果集中行的數(shù)目,僅對(duì)select有效
private $insert_id; //上一步 INSERT 操作產(chǎn)生的 ID
// 構(gòu)造/析構(gòu)函數(shù)
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 連接/選擇數(shù)據(jù)庫(kù)
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('數(shù)據(jù)庫(kù)-連接失?。河脩裘蛎艽a錯(cuò)誤!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("數(shù)據(jù)庫(kù)-選擇失?。簲?shù)據(jù)庫(kù) $this->db_name 不可用");
}
mysql_query("SET NAMES $this->db_charset");
return $this->conn;
}
// query方法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("SQL語(yǔ)句 <b>\"$sql\"</b> 執(zhí)行時(shí)遇到錯(cuò)誤");
return $this->query_id;
}
// 顯示詳細(xì)錯(cuò)誤信息
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "錯(cuò)誤:$msg <br/> 返回:$errinfo<p>";
}else{
echo '<p>出現(xiàn)錯(cuò)誤!<p>';
}
}
// 獲得query執(zhí)行成功與否的信息
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查詢所有
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 獲取 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 獲取 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 顯示共有多少?gòu)埍?
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "數(shù)據(jù)庫(kù) $this->db_name 共有 ".$this->num_rows($this->query_id)." 張表<br/>";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]<br/>";
$i ++;
}
}
}
// 顯示共有多少個(gè)數(shù)據(jù)庫(kù)
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共有數(shù)據(jù)庫(kù) ".$this->num_rows($this->query_id)." 個(gè)<br/>";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[Database]."<br />";
$i ++;
}
}
}
// 刪除數(shù)據(jù)庫(kù):返回刪除結(jié)果
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默認(rèn)刪除當(dāng)前數(shù)據(jù)庫(kù)
$this->query("DROP DATABASE $db_name");
}else {
$this->query("DROP DATABASE $db_name");
}
if ($this->query_id) {
return "數(shù)據(jù)庫(kù) $db_name 刪除成功";
}else {
$this->show_error("數(shù)據(jù)庫(kù) $db_name 刪除失敗");
}
}
// 刪除數(shù)據(jù)表:返回刪除結(jié)果
public function drop_table ($table_name) {
$this->query("DROP TABLE $table_name");
if ($this->query_id) {
return "數(shù)據(jù)表 $table_name 刪除成功";
}else {
$this->show_error("數(shù)據(jù)表 $table_name 刪除失敗");
}
}
// 創(chuàng)建數(shù)據(jù)庫(kù)
public function create_db ($db_name) {
$this->query("CREATE DATABASE $db_name");
if($this->query_id){
return "數(shù)據(jù)庫(kù) $db_name 創(chuàng)建成功";
}else {
$this->show_error("數(shù)據(jù)庫(kù) $db_name 創(chuàng)建失敗");
}
}
// 獲取數(shù)據(jù)庫(kù)版本
public function get_info(){
echo mysql_get_server_info();
}
// 釋放內(nèi)存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // End class
?>
復(fù)制代碼 代碼如下:
<?php
class Mysql {
private $db_host; //主機(jī)地址
private $db_user; //用戶名
private $db_pass; //連接密碼
private $db_name; //名稱
private $db_charset; //編碼
private $conn;
public $debug=false;//調(diào)試開(kāi)關(guān),默認(rèn)關(guān)閉
private $query_id; //用于判斷sql語(yǔ)句是否執(zhí)行成功
private $result; //結(jié)果集
private $num_rows; //結(jié)果集中行的數(shù)目,僅對(duì)select有效
private $insert_id; //上一步 INSERT 操作產(chǎn)生的 ID
// 構(gòu)造/析構(gòu)函數(shù)
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 連接/選擇數(shù)據(jù)庫(kù)
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('數(shù)據(jù)庫(kù)-連接失?。河脩裘蛎艽a錯(cuò)誤!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("數(shù)據(jù)庫(kù)-選擇失?。簲?shù)據(jù)庫(kù) $this->db_name 不可用");
}
mysql_query("SET NAMES $this->db_charset");
return $this->conn;
}
// query方法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("SQL語(yǔ)句 <b>\"$sql\"</b> 執(zhí)行時(shí)遇到錯(cuò)誤");
return $this->query_id;
}
// 顯示詳細(xì)錯(cuò)誤信息
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "錯(cuò)誤:$msg <br/> 返回:$errinfo<p>";
}else{
echo '<p>出現(xiàn)錯(cuò)誤!<p>';
}
}
// 獲得query執(zhí)行成功與否的信息
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查詢所有
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 獲取 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 獲取 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 顯示共有多少?gòu)埍?
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "數(shù)據(jù)庫(kù) $this->db_name 共有 ".$this->num_rows($this->query_id)." 張表<br/>";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]<br/>";
$i ++;
}
}
}
// 顯示共有多少個(gè)數(shù)據(jù)庫(kù)
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共有數(shù)據(jù)庫(kù) ".$this->num_rows($this->query_id)." 個(gè)<br/>";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[Database]."<br />";
$i ++;
}
}
}
// 刪除數(shù)據(jù)庫(kù):返回刪除結(jié)果
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默認(rèn)刪除當(dāng)前數(shù)據(jù)庫(kù)
$this->query("DROP DATABASE $db_name");
}else {
$this->query("DROP DATABASE $db_name");
}
if ($this->query_id) {
return "數(shù)據(jù)庫(kù) $db_name 刪除成功";
}else {
$this->show_error("數(shù)據(jù)庫(kù) $db_name 刪除失敗");
}
}
// 刪除數(shù)據(jù)表:返回刪除結(jié)果
public function drop_table ($table_name) {
$this->query("DROP TABLE $table_name");
if ($this->query_id) {
return "數(shù)據(jù)表 $table_name 刪除成功";
}else {
$this->show_error("數(shù)據(jù)表 $table_name 刪除失敗");
}
}
// 創(chuàng)建數(shù)據(jù)庫(kù)
public function create_db ($db_name) {
$this->query("CREATE DATABASE $db_name");
if($this->query_id){
return "數(shù)據(jù)庫(kù) $db_name 創(chuàng)建成功";
}else {
$this->show_error("數(shù)據(jù)庫(kù) $db_name 創(chuàng)建失敗");
}
}
// 獲取數(shù)據(jù)庫(kù)版本
public function get_info(){
echo mysql_get_server_info();
}
// 釋放內(nèi)存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // End class
?>
您可能感興趣的文章:
- 分享CentOS下MySQL最新版本5.6.13源碼安裝過(guò)程
- mysql-5.5.28源碼安裝過(guò)程中錯(cuò)誤總結(jié)
- apache mysql php 源碼編譯使用方法
- Linux下MySQL 5.5.8 源碼編譯安裝記錄分享
- Mysql源碼學(xué)習(xí)筆記 偷窺線程
- 超級(jí)簡(jiǎn)單的php+mysql留言本源碼
- PHP源碼之 ext/mysql擴(kuò)展部分
- 一個(gè)簡(jiǎn)單的PHP&MYSQL留言板源碼
- 落伍首發(fā) php+mysql 采用ajax技術(shù)的 省 市 地 3級(jí)聯(lián)動(dòng)無(wú)刷新菜單 源碼
- mysql源碼安裝腳本分享
相關(guān)文章
php實(shí)現(xiàn)XSS安全過(guò)濾的方法
這篇文章主要介紹了php實(shí)現(xiàn)XSS安全過(guò)濾的方法,實(shí)例分析了php針對(duì)XSS進(jìn)行安全過(guò)濾的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07淺析Dos下運(yùn)行php.exe,出現(xiàn)沒(méi)有找到php_mbstring.dll 錯(cuò)誤的解決方法
本篇文章是對(duì)在Dos下運(yùn)行php.exe,出現(xiàn)沒(méi)有找到php_mbstring.dll 錯(cuò)誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php中time()與$_SERVER[REQUEST_TIME]用法區(qū)別
這篇文章主要介紹了php中time()與$_SERVER[REQUEST_TIME]用法區(qū)別,詳細(xì)分析了time()與$_SERVER[REQUEST_TIME]的用法,并以實(shí)例形式對(duì)比總結(jié)了二者在獲取當(dāng)前系統(tǒng)時(shí)間戳與請(qǐng)求時(shí)間戳的區(qū)別,需要的朋友可以參考下2014-11-11PHP實(shí)現(xiàn)登陸表單提交CSRF及驗(yàn)證碼
本文主要介紹了PHP實(shí)現(xiàn)登陸表單提交CSRF及驗(yàn)證碼的方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01基于PHP開(kāi)發(fā)中的安全防范知識(shí)詳解
本篇文章是對(duì)PHP開(kāi)發(fā)中的安全防范知識(shí)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP實(shí)現(xiàn)對(duì)文件鎖進(jìn)行加鎖、解鎖操作的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)對(duì)文件鎖進(jìn)行加鎖、解鎖操作的方法,結(jié)合實(shí)例形式分析了PHP針對(duì)文件進(jìn)行加鎖、解鎖操作的功能、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07php 轉(zhuǎn)換字符串編碼 iconv與mb_convert_encoding的區(qū)別說(shuō)明
php 轉(zhuǎn)換字符串編碼 iconv與mb_convert_encoding的區(qū)別說(shuō)明,需要的朋友可以參考下。2011-11-11