《PHP編程:PHP數(shù)據(jù)對(duì)象PDO操作技巧小結(jié)》要點(diǎn):
本文介紹了PHP編程:PHP數(shù)據(jù)對(duì)象PDO操作技巧小結(jié),希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP應(yīng)用本文實(shí)例講述了PHP數(shù)據(jù)對(duì)象PDO操作技巧.分享給大家供大家參考,具體如下:
PHP應(yīng)用PHP 數(shù)據(jù)對(duì)象 (PDO) 擴(kuò)展為PHP訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)定義了一個(gè)輕量級(jí)的一致接口.
PHP應(yīng)用
<?php
try {
$dsn = "mysql:host=localhost; port=3306; dbname=wsq_hotel; charset=utf-8";
$user = 'root';
$psw ='root';
$pdo = new PDO($dsn,$user,$psw);
$sql = 'select goods_prices from wsq_goods_info where goods_id=2';
// $sql = "show database";
$res = $pdo->query($sql) or var_dump($pdo->errorInfo());
// var_dump($res);
$mon = $res->fetch(PDO::FETCH_ASSOC);
echo $mon['goods_price'];
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
PHP應(yīng)用PDO操作事務(wù)
PHP應(yīng)用
//開(kāi)啟事務(wù)
beginTransacition()
//回滾
rollback()
//提交
commit()
//判斷是否處于事務(wù)之中
inTransaction()
PHP應(yīng)用返回最后插入行的ID
PHP應(yīng)用
PDO::lastInsertID()
PHP應(yīng)用exec()執(zhí)行
PHP應(yīng)用與query()相比,exec()返回的是受影響行數(shù)
PHP應(yīng)用
$sql = "insert into table values('$val')";
if(false===$pdo->exec($sql)){
echo '執(zhí)行失敗';
}
PHP應(yīng)用PDO實(shí)現(xiàn)預(yù)編譯
PHP應(yīng)用指的是預(yù)先編譯sql的結(jié)構(gòu)的一種執(zhí)行sql的語(yǔ)法
PHP應(yīng)用如果執(zhí)行多條結(jié)構(gòu)相同的sql,編譯的中間結(jié)果(語(yǔ)法樹(shù))應(yīng)該也是一致的,因此可以將相同的結(jié)構(gòu),統(tǒng)一編譯,每次使用不同的數(shù)據(jù)執(zhí)行即可.
PHP應(yīng)用編譯統(tǒng)一的結(jié)構(gòu)
PHP應(yīng)用
$pdoStatement = $pdo->prepare(sql結(jié)構(gòu))
PHP應(yīng)用綁定數(shù)據(jù)到中間編譯結(jié)果
PHP應(yīng)用
$pdoStatement ->bindValue()
PHP應(yīng)用執(zhí)行
PHP應(yīng)用
$pdoStatement ->execute()
//$sql = "insert into table values(null,?)";
$sql = "insert into table values(null,:name)";
$stmt = $pdo->prepare($sql);
//多組數(shù)據(jù)也是一編譯一執(zhí)行
//$stmt->bindValue(1,'bee');
$stmt->bindValue(':name','bee');
$res = $stmt->execute();
var_dump($res);
PHP應(yīng)用預(yù)編譯能更好地防止sql注入,是因?yàn)轭A(yù)編譯時(shí)候不需要用戶(hù)的數(shù)據(jù)參與,因此編譯時(shí)結(jié)構(gòu)固定,所以數(shù)據(jù)不影響到sql結(jié)構(gòu).
PHP應(yīng)用$pdo->query()與$pdo->execute()如果需要防止sql注入,可以使用$pdo->quote()(其作用是先轉(zhuǎn)義后加引號(hào))
PHP應(yīng)用PDOstatement常用方法:
PHP應(yīng)用errorInfo()
errorCode()
fetchColumn()
fetch()
fetchAll()
rowCount()
closeCursor()
PHP應(yīng)用pdo應(yīng)用
PHP應(yīng)用
<?php
header('content-type:text/html;charset=utf-8');
class PDODB{
static private $_init;
private $_host;
private $_port;
private $_dbname;
private $_username;
private $_password;
private $_charset;
private $_dns;
private $_pdo;
private function __construct($config){
$this->_initParamas($config);
$this->_initDNS();
$this->_initDriverOptions();
$this->_initPDO();
}
private function __clone(){}
static public function getInstance($config){
if(!static::$_init instanceof static){
static::$_init = new static($config);
}
return static::$_init;
}
private function _initParamas($config){
$this->_host = isset($config['host'])?$config['host']:'localhost';
$this->_port = isset($config['port'])?$config['port']:'3306';
$this->_dbname = isset($config['dbname'])?$config['dbname']:'';
$this->_username = isset($config['username'])?$config['username']:'root';
$this->_passward = isset($config['passward'])?$config['passward']:'';
$this->_charset = isset($config['charset'])?$config['charset']:'utf8';
}
private function _initDNS(){
$this->_dns = "mysql:host=$this->_host;port=$this->_port;dbname=$this->_dbname";
}
private function _initDriverOptions(){
$this->_driverOptions = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "set names $this->_charset"
);
}
private function _initPDO(){
$this->_pdo = new PDO($this->_dns,$this->_username,$this->_passward,$this->_driverOptions) or die("fail");
}
public function query($sql){
if(!$result = $this->_pdo->query($sql)){
$erro = $this->_pdo->errorInfo();
echo '失敗的語(yǔ)句'.$sql.'<br>';
echo '錯(cuò)誤代碼'.$erro[1].'<br>';
echo '錯(cuò)誤信息'.$erro[2].'<br>';
die;
}
return $result;
}
public function fetchAll($sql){
$res = $this->query($sql);
$list = $res->fetchAll(PDO::FETCH_ASSOC);
$res->closeCursor();
return $list;
}
public function fetchRow($sql){
$res = $this->query($sql);
$row = $res->fetch(PDO::FETCH_ASSOC);
$res->closeCursor();
return $row;
}
public function fetchOne($sql){
$res = $this->query($sql);
$one = $res->fetchColumn();
$res->closeCursor();
return $one;
}
public function escape_string($data){
return $this->_pdo->quote($data);
}
}
$config = array(
"host"=>"localhost",
"username"=>"root",
"passward"=>"root",
"dbname"=>"students"
);
$pdo = PDODB::getInstance($config);
$sql = "select sdept from student where sage=21";
var_dump($pdo->fetchRow($sql));
?>
PHP應(yīng)用運(yùn)行效果圖如下:
PHP應(yīng)用
PHP應(yīng)用更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
PHP應(yīng)用希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/3194.html