PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類
本文實(shí)例講述了PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類。分享給大家供大家參考,具體如下:
配置文件:
<?php $db = array( 'host'=>'localhost', 'user'=>'root', 'password'=>'', 'database'=>'test', ) ?>
php 數(shù)據(jù)庫(kù)基類:
<?php class db { public $conn; public static $sql; public static $instance=null; private function __construct(){ require_once('db.config.php'); $this->conn = mysql_connect($db['host'],$db['user'],$db['password']); if(!mysql_select_db($db['database'],$this->conn)){ echo "失敗"; }; mysql_query('set names utf8',$this->conn); } public static function getInstance(){ if(is_null(self::$instance)){ self::$instance = new db; } return self::$instance; } /** * 查詢數(shù)據(jù)庫(kù) */ public function select($table,$condition=array(),$field = array()){ $where=''; if(!empty($condition)){ foreach($condition as $k=>$v){ $where.=$k."='".$v."' and "; } $where='where '.$where .'1=1'; } $fieldstr = ''; if(!empty($field)){ foreach($field as $k=>$v){ $fieldstr.= $v.','; } $fieldstr = rtrim($fieldstr,','); }else{ $fieldstr = '*'; } self::$sql = "select {$fieldstr} from {$table} {$where}"; $result=mysql_query(self::$sql,$this->conn); $resuleRow = array(); $i = 0; while($row=mysql_fetch_assoc($result)){ foreach($row as $k=>$v){ $resuleRow[$i][$k] = $v; } $i++; } return $resuleRow; } /** * 添加一條記錄 */ public function insert($table,$data){ $values = ''; $datas = ''; foreach($data as $k=>$v){ $values.=$k.','; $datas.="'$v'".','; } $values = rtrim($values,','); $datas = rtrim($datas,','); self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})"; if(mysql_query(self::$sql)){ return mysql_insert_id(); }else{ return false; }; } /** * 修改一條記錄 */ public function update($table,$data,$condition=array()){ $where=''; if(!empty($condition)){ foreach($condition as $k=>$v){ $where.=$k."='".$v."' and "; } $where='where '.$where .'1=1'; } $updatastr = ''; if(!empty($data)){ foreach($data as $k=>$v){ $updatastr.= $k."='".$v."',"; } $updatastr = 'set '.rtrim($updatastr,','); } self::$sql = "update {$table} {$updatastr} {$where}"; return mysql_query(self::$sql); } /** * 刪除記錄 */ public function delete($table,$condition){ $where=''; if(!empty($condition)){ foreach($condition as $k=>$v){ $where.=$k."='".$v."' and "; } $where='where '.$where .'1=1'; } self::$sql = "delete from {$table} {$where}"; return mysql_query(self::$sql); } public static function getLastSql(){ echo self::$sql; } } $db = db::getInstance(); //$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password')); //echo $db->insert('demo',array('name'=>'腳本之家','password'=>'123')); //echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1)); echo $db->delete('demo',array('id'=>'2')); db::getLastSql(); echo "<pre>"; ?>
更多關(guān)于PHP操作數(shù)據(jù)庫(kù)相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫(kù)操作入門教程》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP單例模式數(shù)據(jù)庫(kù)連接類與頁(yè)面靜態(tài)化實(shí)現(xiàn)方法
- PHP中數(shù)據(jù)庫(kù)單例模式的實(shí)現(xiàn)代碼分享
- PHP單例模式應(yīng)用示例【多次連接數(shù)據(jù)庫(kù)只實(shí)例化一次】
- PHP實(shí)現(xiàn)單例模式最安全的做法
- php單例模式的簡(jiǎn)單實(shí)現(xiàn)方法
- php利用單例模式實(shí)現(xiàn)日志處理類庫(kù)
- PHP基于單例模式編寫PDO類的方法
- PHP最常用的2種設(shè)計(jì)模式工廠模式和單例模式介紹
- PHP設(shè)計(jì)模式之工廠模式與單例模式
- PHP實(shí)現(xiàn)單例模式建立數(shù)據(jù)庫(kù)連接的方法分析
相關(guān)文章
php數(shù)組函數(shù)序列 之a(chǎn)rray_count_values() 統(tǒng)計(jì)數(shù)組中所有值出現(xiàn)的次數(shù)函數(shù)
array_count_values() 函數(shù)用于統(tǒng)計(jì)數(shù)組中所有值出現(xiàn)的次數(shù),本函數(shù)返回一個(gè)數(shù)組,其元素的鍵名是原數(shù)組的值,鍵值是該值在原數(shù)組中出現(xiàn)的次數(shù)。2011-10-10學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)建造者模式
這篇文章主要介紹了php設(shè)計(jì)模式中的建造者模式,使用php實(shí)現(xiàn)建造者模式,感興趣的小伙伴們可以參考一下2015-12-12計(jì)算php頁(yè)面運(yùn)行時(shí)間的函數(shù)介紹
本篇文章是對(duì)計(jì)算php頁(yè)面運(yùn)行時(shí)間的函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07php獲取從百度搜索進(jìn)入網(wǎng)站的關(guān)鍵詞的詳細(xì)代碼
以下是關(guān)于php該如何獲取從百度搜索進(jìn)入網(wǎng)站的關(guān)鍵詞的詳細(xì)代碼,希望本文對(duì)廣大php開發(fā)者有所幫助2014-01-01PHP入門教程之使用Mysqli操作數(shù)據(jù)庫(kù)的方法(連接,查詢,事務(wù)回滾等)
這篇文章主要介紹了PHP入門教程之使用Mysqli操作數(shù)據(jù)庫(kù)的方法,涉及php+mysqli操作數(shù)據(jù)庫(kù)的基本連接、編碼設(shè)置、查詢、修改、事務(wù)回滾等操作技巧,需要的朋友可以參考下2016-09-09