2024 日本 PHP 研討會

PHP 資料物件 (PDO)

新增註解

使用者貢獻的註解 4 則註解

djlopez at gmx dot de
18 年前
請注意

這段程式碼無法運作
$sth = $dbh->prepare('SELECT name, colour, calories FROM ? WHERE calories < ?');

這段程式碼可以運作!
$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ?');

參數不能用於表格名稱!
wiserufferto at gmail dot com
3 年前
有點晚了... 但我年紀大了,動作比較慢.......
關於繼承 PDOStatement 和 PDO,我發現以傳址方式傳遞 PDOExtended 類別會有幫助
在建構子中,於 parent::__construct() 之後
$this->setAttribute(\PDO::ATTR_STATEMENT_CLASS,array('PDOStatementExtended', [&$this]));}

以及在
class PDOStatementExtended extends \PDOStatement
{

protected function __construct
(
\PDO &$PDO,
)
pokojny at radlight dot com
18 年前
我想要繼承 PDO 類別來儲存資料庫使用情況的統計資訊,但我遇到了一些問題。我想要計算建立的語句數量以及它們執行的次數。因此 PDOStatement 應該要連結到建立它的 PDO 並儲存統計資訊。問題是我不知道 PDO 如何建立 PDOStatement(建構子參數等等),所以我建立了這兩個類別

<?php
/**
* PHP Document Object plus
*
* PHP Document Object plus is library with functionality of PDO, entirely written
* in PHP, so that developer can easily extend it's classes with specific functionality,
* such as providing database usage statistics implemented in v1.0b
*
* @author Peter Pokojny
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class PDOp {
protected
$PDO;
public
$numExecutes;
public
$numStatements;
public function
__construct($dsn, $user=NULL, $pass=NULL, $driver_options=NULL) {
$this->PDO = new PDO($dsn, $user, $pass, $driver_options);
$this->numExecutes = 0;
$this->numStatements = 0;
}
public function
__call($func, $args) {
return
call_user_func_array(array(&$this->PDO, $func), $args);
}
public function
prepare() {
$this->numStatements++;

$args = func_get_args();
$PDOS = call_user_func_array(array(&$this->PDO, 'prepare'), $args);

return new
PDOpStatement($this, $PDOS);
}
public function
query() {
$this->numExecutes++;
$this->numStatements++;

$args = func_get_args();
$PDOS = call_user_func_array(array(&$this->PDO, 'query'), $args);

return new
PDOpStatement($this, $PDOS);
}
public function
exec() {
$this->numExecutes++;

$args = func_get_args();
return
call_user_func_array(array(&$this->PDO, 'exec'), $args);
}
}
class
PDOpStatement implements IteratorAggregate {
protected
$PDOS;
protected
$PDOp;
public function
__construct($PDOp, $PDOS) {
$this->PDOp = $PDOp;
$this->PDOS = $PDOS;
}
public function
__call($func, $args) {
return
call_user_func_array(array(&$this->PDOS, $func), $args);
}
public function
bindColumn($column, &$param, $type=NULL) {
if (
$type === NULL)
$this->PDOS->bindColumn($column, $param);
else
$this->PDOS->bindColumn($column, $param, $type);
}
public function
bindParam($column, &$param, $type=NULL) {
if (
$type === NULL)
$this->PDOS->bindParam($column, $param);
else
$this->PDOS->bindParam($column, $param, $type);
}
public function
execute() {
$this->PDOp->numExecutes++;
$args = func_get_args();
return
call_user_func_array(array(&$this->PDOS, 'execute'), $args);
}
public function
__get($property) {
return
$this->PDOS->$property;
}
public function
getIterator() {
return
$this->PDOS;
}
}
?>

這些類別具有包含原始 PDO 和 PDOStatement 物件的屬性,這些物件提供 PDOp 和 PDOpStatement 的功能。
從外部來看,PDOp 和 PDOpStatement 看起來像 PDO 和 PDOStatement,但也提供所需的資訊。
www.navin.biz
18 年前
以下是繼承 PDO 和 PDOStatement 類別的範例

<?php

類別 Database 繼承 PDO
{
函式
__construct()
{
parent::__construct('mysql:dbname=test;host=localhost', 'root', '');
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DBStatement', array($this)));
}
}

類別
DBStatement 繼承 PDOStatement
{
公開
$dbh;

受保護 函式
__construct($dbh)
{
$this->dbh = $dbh;
$this->setFetchMode(PDO::FETCH_OBJ);
}

公開 函式
foundRows()
{
$rows = $this->dbh->prepare('SELECT found_rows() AS rows', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE));
$rows->execute();
$rowsCount = $rows->fetch(PDO::FETCH_OBJ)->rows;
$rows->closeCursor();
回傳
$rowsCount;
}
}

?>
To Top