PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫操作基類
本文實(shí)例講述了PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫操作基類。分享給大家供大家參考,具體如下:
配置文件:
<?php
$db = array(
'host'=>'localhost',
'user'=>'root',
'password'=>'',
'database'=>'test',
)
?>
php 數(shù)據(jù)庫基類:
<?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ù)庫
*/
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ù)庫相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實(shí)現(xiàn)方法
- PHP中數(shù)據(jù)庫單例模式的實(shí)現(xiàn)代碼分享
- PHP單例模式應(yīng)用示例【多次連接數(shù)據(jù)庫只實(shí)例化一次】
- PHP實(shí)現(xiàn)單例模式最安全的做法
- php單例模式的簡(jiǎn)單實(shí)現(xiàn)方法
- php利用單例模式實(shí)現(xiàn)日志處理類庫
- PHP基于單例模式編寫PDO類的方法
- PHP最常用的2種設(shè)計(jì)模式工廠模式和單例模式介紹
- PHP設(shè)計(jì)模式之工廠模式與單例模式
- PHP實(shí)現(xiàn)單例模式建立數(shù)據(jù)庫連接的方法分析
相關(guān)文章
Ubuntu中支持PHP5與PHP7雙版本的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Ubuntu中支持PHP5與PHP7雙版本的簡(jiǎn)單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),這個(gè)方法也非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
php select,radio和checkbox默認(rèn)選擇的實(shí)現(xiàn)方法
radio和checkbox默認(rèn)選擇的實(shí)現(xiàn)方法,大家參考下原理就知道了,不論asp,asp.net,jsp都是這個(gè)原理。2010-05-05
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)(OOP)之方法重寫(override)操作示例
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)(OOP)之方法重寫(override)操作,簡(jiǎn)單描述了php面向?qū)ο蟪绦蛟O(shè)計(jì)中方法重寫的原理,并結(jié)合實(shí)例形式分析了php方法重寫相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12
PHP正則表達(dá)式 /i, /is, /s, /isU等介紹
PHP正則表達(dá)式 /i, /is, /s, /isU等,都代表著什么意思,你知道嗎?下面為大家詳細(xì)介紹下2014-10-10
使用PHP實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)可視化功能的示例詳解
實(shí)時(shí)數(shù)據(jù)可視化功能,是指在Web應(yīng)用程序開發(fā)中,將服務(wù)器發(fā)送的實(shí)時(shí)數(shù)據(jù),本文將介紹如何使用PHP和前端框架實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)可視化功能,需要的可以參考一下2023-07-07
PHP中static關(guān)鍵字原理的學(xué)習(xí)研究分析
PHP中static關(guān)鍵字原理的學(xué)習(xí)研究分析,學(xué)習(xí)php的朋友可以參考下。2011-07-07
php查詢mysql數(shù)據(jù)庫并將結(jié)果保存到數(shù)組的方法
這篇文章主要介紹了php查詢mysql數(shù)據(jù)庫并將結(jié)果保存到數(shù)組的方法,實(shí)例分析了php使用mysql_fetch_assoc查詢數(shù)據(jù)庫的技巧,需要的朋友可以參考下2015-03-03

