封裝PDO操作資料庫
1,建立檔案MysqlPdo.class.php檔案用來封裝PDO作業資料庫
具體程式碼如下:
<?php
class MysqlPdo{
private $dbConfig=array(
'db'=>'mysql',
'host'=>'localhost',
'port'=>'3306',
'user'=>'root',
'pwd'=>'',
'charset'=>'utf8',
'dbname'=>''
);
private static $instance; //单例模式
private $db; //PDO实例
private $data=array(); //操作数据
private function __construct($params)
{
$this->dbConfig=array_merge($this->dbConfig,$params);
$this->connect();
}
//连接服务器
private function connect(){
//mysql:host=localhost
//mysql:host:localhost;port=3306;dbname=php;charset=utf-8
$dsn="{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']};charset={$this->dbConfig['charset']}}";
try{
//实例化PDO
$this->db=new PDO($dsn,$this->dbConfig['user'],$this->dbConfig['pwd']);
}catch (PDOException $exception){
die("数据库连接失败");
}
}
public static function getInstance($params=array()){
if(!self::$instance instanceof self){
self::$instance=new self($params);
}
return self::$instance; //返回对象
}
//私有化克隆,防止外部调用clone $对象 生成新的对象,因为是单例模式
private function __clone()
{
// TODO: Implement __clone() method.
}
//通过预处理方式执行sql
public function query($sql,$batch=false){
$data=$batch?$this->data:array($this->data);
$this->data=array();
//通过预处理方式执行SQL
$stmt=$this->db->prepare($sql);
foreach($data as $v){
if($stmt->execute($v)===false){
die("数据库PDO预处理操作失败");
}
}
return $stmt;
}
public function data($data){
$this->data=$data;
return $this; //返回对象自身用于连贯操作
}
//取得一行结果
public function fetchRow($sql){
return $this->query($sql)->fetch(PDO::FETCH_ASSOC);//返回索引数组
}
//取得多行结果
public function fetchAll($sql){
return $this->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
} 1,本封裝類別採用單例模式,私有化了建構方法與複製方法,保證只能有一個實例化物件,節約資料庫連線資源,
#2,在每次建立資料庫連接物件時必須呼叫getInstance(),在getInstance裡傳入資料庫配置資訊用於在實例化物件的時候自動呼叫建構函式使傳入的資料庫配置訊息與原來的資訊進行合併,方便擴展其他資料庫的連接,更換資料庫也只需要更改配置資訊即可
3,透過預處理方式執行sql語句

4,連貫運算的使用

5,處理查詢的結果集

#以上便是對資料庫類別的封裝處理
#


