PHP單例模式簡單用法示例
本文實(shí)例講述了PHP單例模式用法。分享給大家供大家參考,具體如下:
<?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 self();
}
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 = '';
$data = '';
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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)單例模式最安全的做法
- php單例模式實(shí)現(xiàn)(對(duì)象只被創(chuàng)建一次)
- php利用單例模式實(shí)現(xiàn)日志處理類庫
- PHP最常用的2種設(shè)計(jì)模式工廠模式和單例模式介紹
- php設(shè)計(jì)模式 Singleton(單例模式)
- php基于單例模式封裝mysql類完整實(shí)例
- PHP基于單例模式實(shí)現(xiàn)的mysql類
- PHP基于單例模式編寫PDO類的方法
- php設(shè)計(jì)模式 Template (模板模式)
- PHP設(shè)計(jì)模式之觀察者模式(Observer)詳細(xì)介紹和代碼實(shí)例
- php設(shè)計(jì)模式 Proxy (代理模式)
- php設(shè)計(jì)模式小結(jié)
相關(guān)文章
PHP自定義函數(shù)實(shí)現(xiàn)assign()數(shù)組分配到模板及extract()變量分配到模板功能示例
這篇文章主要介紹了PHP自定義函數(shù)實(shí)現(xiàn)assign()數(shù)組分配到模板及extract()變量分配到模板功能,可模擬tp框架中模板變量分配功能,涉及php基于面向?qū)ο蟮臄?shù)組賦值相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
php中mkdir()函數(shù)的權(quán)限問題分析
這篇文章主要介紹了php中mkdir()函數(shù)的權(quán)限問題分析,需要的朋友可以參考下2016-09-09
PHP高級(jí)對(duì)象構(gòu)建 工廠模式的使用
工廠模式包含普通工廠模式和抽象工廠模式,但是,不管是什么工廠模式,它們都是有一個(gè)作用,那就是生成對(duì)象2012-02-02
原生JS實(shí)現(xiàn)Ajax通過POST方式與PHP進(jìn)行交互的方法示例
這篇文章主要介紹了原生JS實(shí)現(xiàn)Ajax通過POST方式與PHP進(jìn)行交互的方法,涉及ajax使用post方式與后臺(tái)交互及php數(shù)據(jù)接收、處理、查詢數(shù)據(jù)庫等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
PHP實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)功能,結(jié)合實(shí)例形式分析了php基于session對(duì)象計(jì)算與統(tǒng)計(jì)在線人數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10

