php封裝的連接Mysql類及用法分析
更新時間:2015年12月10日 10:20:26 作者:happy664618843
這篇文章主要介紹了php封裝的連接Mysql類及用法,基于php封裝了簡單的MySQL數(shù)據(jù)庫的連接、查詢、遍歷等技巧,并附帶說明了其具體用法,需要的朋友可以參考下
本文實例講述了php封裝的連接Mysql類及用法。分享給大家供大家參考,具體如下:
class mysql{
private $db_name;
private $db_host;
private $db_user;
private $db_pwd;
private $conn;
private $querysql;
private $result;
private $resultarray=array();
private $row;
//創(chuàng)建構(gòu)造函數(shù) 數(shù)據(jù)庫名 主機名 用戶名 密碼
function __counstruct($dbname,$dbhost,$dbuser,$dbpwd){
$this->db_name=$dbname;
$this->db_host=$dbhost;
$this->db_pwd=$dbpwd;
$this->db_user=$dbuser;
$this->dbconnect();
$this->selectdb();
}
//連接數(shù)據(jù)庫
private function db_connect(){
$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd) or die("Could not Connect MySql Server");
}
private function selectdb(){
mysql_select_db($this->db_name) or die("unable to select dbname")
}
private function query(){
return $this->result=mysql_query($this->querysql);
}
private function get_result($sql){
$this->querysql=$sql;
$this->query();
if($this->get_num()>0){
//mysql_fetch_assoc()和 mysql_fetch_array(,MYSQL_ASSOC)從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組 沒有則返回false
while($this->rows=mysql_fetch_array($this->result)){
//賦值 數(shù)組賦值 resultarray[]= 將影響的行數(shù)賦值給數(shù)組
$this->resultarray[]=$this->rows
}
return $this->resultarray;
}
}
//$result返回值為 bool類型 false為沒有數(shù)據(jù)
private function get_num(){
return $this->num=mysql_num_rows($this->result);
}
}
$m=new mysql("testuser","localhost","root","root");
$arreresult=$m->get_result("select * from userinfo");
希望本文所述對大家php程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫操作類與用法示例
- PHP封裝的mysqli數(shù)據(jù)庫操作類示例
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】
- php基于PDO實現(xiàn)功能強大的MYSQL封裝類實例
- php基于單例模式封裝mysql類完整實例
- php封裝的mysqli類完整實例
- php mysql 封裝類實例代碼
- php實現(xiàn)mysql封裝類示例
- PHP訪問MYSQL數(shù)據(jù)庫封裝類(附函數(shù)說明)
- php鏈?zhǔn)讲僮鱩ysql數(shù)據(jù)庫(封裝類帶使用示例)
相關(guān)文章
PHP在不同頁面間傳遞Json數(shù)據(jù)示例代碼
本文為大家介紹下PHP如何在不同頁面間傳遞Json數(shù)據(jù),具體實現(xiàn)如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-06-06
windows服務(wù)器中檢測PHP SSL是否開啟以及開啟SSL的方法
這篇文章主要介紹了windows服務(wù)器中檢測PHP SSL是否開啟以及開啟SSL的方法,需要的朋友可以參考下2014-04-04
php進程(線程)通信基礎(chǔ)之System V共享內(nèi)存簡單實例分析
這篇文章主要介紹了php進程(線程)通信基礎(chǔ)之System V共享內(nèi)存,結(jié)合簡單實例形式分析了PHP System V共享內(nèi)存原理、相關(guān)函數(shù)與基本使用技巧,需要的朋友可以參考下2019-11-11

