- 本文可以結(jié)合我的文章設(shè)計(jì)模式中的-單例模式
再進(jìn)行封裝下
使用類時(shí)使用以下代碼即可
$pdo = DBPDO::get_instance();
$pdo->test();
$user='root';
$pass='123456';
$dsn='mysql:host=localhost;dbname=test';
//$pdo = new PDO($dsn,$user,$pass);
//
try{
//創(chuàng)建pdo對(duì)象
$pdo=new PDO($dsn,$user,$pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE,2);//異常報(bào)錯(cuò)
$pdo->exec("set names utf8");
}catch(PDOException $e){
echo $e->getMessage();
}
//$sql="insert into dlxxx(payamount,username,remark,orderid,payip,paytype,returnurl,notifyurl,version,consdition)values(?,?,?,?,?,?,?,?,?,?)";
$sql = "insert into users(username,password,amount) values(?,?,?)";
$stmt = $pdo->prepare($sql);
$aa = '2';
$stmt->bindParam(1,$user);//不能直接給數(shù)據(jù)
$stmt->bindParam(2,$pass);
$stmt->bindParam(3,$aa);
$ss = $stmt->execute();
var_dump($ss);
注意
bindParam()
綁定的參數(shù)必須是一個(gè)變量 ,bindValue()
既可以綁定變量葵萎,又可以綁定具體值
PDO連接數(shù)據(jù)庫的類
<?php
//定義數(shù)據(jù)庫信息
header("Content-type:text/html; charset=utf-8");
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PWD', '123456');
define('DB_NAME', 'test');
class DBPDO {
private static $instance;
public $dsn;
public $dbuser;
public $dbpwd;
public $sth;
public $dbh;
//初始化
function __construct() {
$this->dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME;
$this->dbuser = DB_USER;
$this->dbpwd = DB_PWD;
$this->connect();
$this->dbh->query("SET NAMES 'UTF8'");
$this->dbh->query("SET TIME_ZONE = '+8:00'");
}
//連接數(shù)據(jù)庫
public function connect() {
try {
$this->dbh = new PDO($this->dsn, $this->dbuser, $this->dbpwd);
}
catch(PDOException $e) {
exit('連接失敗:'.$e->getMessage());
}
}
//獲取表字段
public function getFields($table='vista_order') {
$this->sth = $this->dbh->query("DESCRIBE $table");
$this->getPDOError();
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $this->sth->fetchAll();
$this->sth = null;
return $result;
}
//插入數(shù)據(jù)
public function insert($sql) {
if($this->dbh->exec($sql)) {
$this->getPDOError();
return $this->dbh->lastInsertId();
}
return false;
}
//刪除數(shù)據(jù)
public function delete($sql) {
if(($rows = $this->dbh->exec($sql)) > 0) {
$this->getPDOError();
return $rows;
}
else {
return false;
}
}
//更改數(shù)據(jù)
public function update($sql) {
if(($rows = $this->dbh->exec($sql)) > 0) {
$this->getPDOError();
return $rows;
}
return false;
}
//獲取數(shù)據(jù)
public function select($sql) {
$this->sth = $this->dbh->query($sql);
$this->getPDOError();
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $this->sth->fetchAll();
$this->sth = null;
return $result;
}
//獲取數(shù)目
public function count($sql) {
$count = $this->dbh->query($sql);
$this->getPDOError();
return $count->fetchColumn();
}
//獲取PDO錯(cuò)誤信息
private function getPDOError() {
if($this->dbh->errorCode() != '00000') {
$error = $this->dbh->errorInfo();
exit($error[2]);
}
}
//關(guān)閉連接
public function __destruct() {
$this->dbh = null;
}
}
//eg: an example for operate select
$test = new DBPDO;
$sql = "SELECT * FROM `users`";
$rs = $test->select($sql);
print_r($rs);
注意导犹,插入語句的準(zhǔn)備,不可以直接$pdo->prepare($sql),因?yàn)榇藭r(shí)的$pdo已經(jīng)是對(duì)象實(shí)例化之后的PDO類羡忘,而類里面沒有prepare()這個(gè)函數(shù)
$pdo->dbh才相當(dāng)于我們使用的$pdo
$stmt = $pdo->dbh->prepare($sql);
?>