PHP单例模式简单用法示例
作者:北京流浪儿 时间:2023-11-18 19:45:41
本文实例讲述了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;
}
/**
* 查询数据库
*/
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>";
?>
希望本文所述对大家PHP程序设计有所帮助。
标签:PHP,单例模式
0
投稿
猜你喜欢
python实现一个简单的udp通信的示例代码
2023-07-23 16:08:43
ajax+node+request爬取网络图片的实例(宅男福利)
2024-05-05 09:21:11
golang的HTTP基本认证机制实例详解
2024-04-26 17:28:39
ASP.NET(AJAX+JSON)实现对象调用
2023-07-19 12:29:33
Python灰度变换中灰度切割分析实现
2021-10-05 12:07:49
Python Selenium安装及环境配置的实现
2023-01-02 18:19:18
Python用list或dict字段模式读取文件的方法
2022-07-16 18:29:07
快速解决 MySQL中与浮点比较有关的问题
2008-11-27 16:28:00
JS实现TITLE悬停长久显示效果完整示例
2024-04-16 09:54:00
详解Python用三种方式统计词频的方法
2021-02-10 13:30:12
别人复制你网站的文章时自动加上注释
2009-02-09 13:20:00
Python实现图像随机添加椒盐噪声和高斯噪声
2023-06-13 22:54:36
解决django model修改添加字段报错的问题
2021-06-18 12:06:38
WebSocket部署到服务器出现连接失败问题的分析与解决
2023-08-15 22:43:21
ORACLE分区表转换在线重定义DBMS_REDEFINITION
2024-01-16 00:23:18
Python PyQt5模块实现一个浏览器的示例代码
2022-02-22 04:23:47
Ajax改造:使用Ajax和jQuery改进现有站点
2010-04-02 12:50:00
JS数组合并push与concat区别分析
2024-04-22 22:40:09
Python json模块dumps、loads操作示例
2023-01-03 20:16:29
zabbix监控MySQL主从状态的方法详解
2024-01-22 00:29:24