单例基础知识简介 案例
连接数据库时使用单例模式,避免造成对数据库反复连接造成的浪费!
主要分两部分
第一部分:数据库连接的单例
第二部分:DB类的调用
1.数据库的连接
代码语言:javascript复制<?php
class Database
{
private $pdo;
static $instance;
private function __construct()
{
//此处用的是常量,可替换中自己对应的数据库
$this->pdo = new PDO(DSN,USER,PWD);
}
private function __clone()
{
}
public static function getInstance()
{
if(!(self::$instance instanceof self)){
self::$instance = new self();
}
return self::$instance;
}
public function getPDO()
{
return $this->pdo;
}
}
$database = Database::getInstance();
$pdo = $database->getPDO();
?>
2.DB类调用连接
代码语言:javascript复制<?php
// 数据库类,其他类都可用
class DB
{
// pdo对象
private $pdo;
// 字段名
private $field = '*';
// 表名
private $table;
// 条件
private $where;
// 分组
private $group;
// 筛选
private $having;
// 排序
private $order;
// 分页
private $limit;
// sql源生语句
public $sql;
// 链接数据库
public function __construct()
{
// $this->pdo = new PDO(DSN, USER, PWD);
$database = Database::getInstance();
$this->pdo = $database->getPDO();
}
// 查询数据
public function select()
{
$this->sql = 'select '.$this->field.' from '.$this->table.$this->where.$this->group.$this->having.$this->order.$this->limit;
$this->init();
$res = $this->pdo->query($this->sql);
if(is_object($res)){
$data = $res->fetchAll(PDO::FETCH_ASSOC);
return $data;
}
return false;
}
// 单数据查询
public function find()
{
$this->sql = ' select '.$this->field.' from '.$this->table.$this->where;
$this->init();
$res = $this->pdo->query($this->sql);
if(is_object($res)){
$data = $res->fetch(PDO::FETCH_ASSOC);
return $data;
}
return false;
}
// 删除数据
public function delete()
{
$this->sql = 'delete from '.$this->table.$this->where.$this->order.$this->limit;
$this->init();
$res = $this->pdo->exec($this->sql);
return $res;
}
// 插入数据
public function insert($data = '')
{
if(!is_array($data)){
return false;
}
// 准备sql
$field = null;
$value = null;
// 拼接之前, 保证$data 里的数据 不用再调整
foreach($data as $k => $v){
$field .= '`'.$k.'`,';
$value .= '"'.$v.'",';
}
$field = rtrim($field, ',');
$value = rtrim($value, ',');
$this->sql = 'insert into '.$this->table.' ( '.$field.' ) values('.$value.')';
// 执行sql
$res = $this->pdo->exec($this->sql);
if($res){
$newId = $this->pdo->lastInsertId();
return $newId;
}else{
return false;
}
}
// 更新数据
public function update($data = '')
{
if(!is_array($data)){
return false;
}
// 准备sql
$str = null;
// 拼接之前, 保证$data 里的数据 不用再调整
foreach($data as $k => $v){
$str .= '`'.$k.'`="'.$v.'"'.',';
}
$str = rtrim($str, ',');
$this->sql = 'update '.$this->table.' set '.$str.$this->where;
$this->init();
// 执行sql
$res = $this->pdo->exec($this->sql);
if($res){
return $res;
}else{
return false;
}
}
public function field( $param = '')
{
if( $param == '' ){
$this->field = '*';
}else{
$this->field = $param;
}
return $this;
}
public function table($param = '')
{
$this->table = $param;
return $this;
}
public function where($param = '')
{
if($param == ''){
$this->where = null;
}else{
$this->where = ' where '.$param;
}
return $this;
}
public function group($param = '')
{
if($param == ''){
$this->group = null;
}else{
$this->group = ' group by '.$param;
}
return $this;
}
public function having($param = '')
{
if($param == ''){
$this->having = null;
}else{
$this->having = ' having '.$param;
}
return $this;
}
public function order($param = '')
{
if($param == ''){
$this->order = null;
}else{
$this->order = ' order by '.$param;
}
return $this;
}
public function limit($param = '')
{
if($param == ''){
$this->limit = null;
}else{
$this->limit = ' limit '.$param;
}
return $this;
}
// 初始化所有字段
public function init()
{
$this->field = '*';
$this->table = null;
$this->where = null;
$this->group = null;
$this->having = null;
$this->order = null;
$this->limit = null;
}
}
?>