PDOStatement 类
(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 1.0.0)
简介
代表一条预处理语句,并在该语句被执行后代表一个相关的结果集。
类摘要
class PDOStatement
implements
Traversable {
/* 属性 */
/* 方法 */
bindColumn(
mixed
mixed
int
int
mixed
): bool
mixed
$column
,mixed
&$param
,int
$type
= ?,int
$maxlen
= ?,mixed
$driverdata
= ?): bool
bindParam(
mixed
mixed
int
int
mixed
): bool
mixed
$parameter
,mixed
&$variable
,int
$data_type
= PDO::PARAM_STR,int
$length
= ?,mixed
$driver_options
= ?): bool
closeCursor(): bool
columnCount(): int
debugDumpParams(): bool
errorCode(): string
errorInfo(): array
fetch(int
$fetch_style
= ?, int $cursor_orientation
= PDO::FETCH_ORI_NEXT, int $cursor_offset
= 0): mixednextRowset(): bool
rowCount(): int
}属性
- queryString
-
所用的查询字符串
目录
- PDOStatement::bindColumn — 绑定一列到一个 PHP 变量
- PDOStatement::bindParam — 绑定一个参数到指定的变量名
- PDOStatement::bindValue — 把一个值绑定到一个参数
- PDOStatement::closeCursor — 关闭游标,使语句能再次被执行。
- PDOStatement::columnCount — 返回结果集中的列数
- PDOStatement::debugDumpParams — 打印一条 SQL 预处理命令
- PDOStatement::errorCode — 获取跟上一次语句句柄操作相关的 SQLSTATE
- PDOStatement::errorInfo — 获取跟上一次语句句柄操作相关的扩展错误信息
- PDOStatement::execute — 执行一条预处理语句
- PDOStatement::fetch — 从结果集中获取下一行
- PDOStatement::fetchAll — 返回一个包含结果集中所有行的数组
- PDOStatement::fetchColumn — 从结果集中的下一行返回单独的一列。
- PDOStatement::fetchObject — 获取下一行并作为一个对象返回。
- PDOStatement::getAttribute — 检索一个语句属性
- PDOStatement::getColumnMeta — 返回结果集中一列的元数据
- PDOStatement::getIterator — Gets result set iterator
- PDOStatement::nextRowset — 在一个多行集语句句柄中推进到下一个行集
- PDOStatement::rowCount — 返回受上一个 SQL 语句影响的行数
- PDOStatement::setAttribute — 设置一个语句属性
- PDOStatement::setFetchMode — 为语句设置默认的获取模式。

User Contributed Notes 2 notes
Gino D. ¶
4 years ago
I don't know why PDOStatement don't return "execution time" and "found rows" so here I created an extended class of PDOStatement with these attributes.
Just have to "setAttribute" of PDO's object to $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS , ['\customs\PDOStatement', [&$this]]);
<?php
/**
*
*
*
*/
namespace customs;
/**
*
*
*
*/
final class PDOStatement extends \PDOStatement {
/**
*
*
*
*/
protected $PDO = null;
protected $inputParams = [];
protected $executionTime = 0;
protected $resultCount = 0;
/**
*
*
*
*/
protected function __construct(PDO &$PDO) {
$this->PDO = $PDO;
$this->executionTime = microtime(true);
}
/**
*
*
*
*/
final public function getExecutionError(int $i = 2) {
$executionError = $this->errorInfo();
if (isset($executionError[$i]))
return $executionError[$i];
return $executionError;
}
/**
*
*
*
*/
final public function getExecutionTime($numberFormat = false, $decPoint = '.', $thousandsSep = ',') {
if (is_numeric($numberFormat))
return number_format($this->executionTime, $numberFormat, $decPoint, $thousandsSep);
return $this->executionTime;
}
/**
*
*
*
*/
final public function getResultCount($numberFormat = false, $decPoint = '.', $thousandsSep = ',') {
if (is_numeric($numberFormat))
return number_format($this->resultCount, $numberFormat, $decPoint, $thousandsSep);
return $this->resultCount;
}
/**
*
*
*
*/
final public function getLastInsertId() {
return $this->PDO->lastInsertId();
}
/**
*
*
*
*/
final public function bindValues(array $inputParams) {
foreach ($this->inputParams = array_values($inputParams) as $i => $value) {
$varType = is_null($value) ? \PDO::PARAM_NULL : is_bool($value) ? \PDO::PARAM_BOOL : is_int($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
if (!$this->bindValue(++ $i, $value, $varType))
return false;
}
return true;
}
/**
*
*
*
*/
final public function execute($inputParams = null) {
if ($inputParams)
$this->inputParams = $inputParams;
if ($executed = parent::execute($inputParams))
$this->executionTime = microtime(true) - $this->executionTime;
return $executed;
}
/**
*
*
*
*/
final public function fetchAll($how = null, $className = null, $ctorArgs = null) {
$resultSet = parent::fetchAll(... func_get_args());
if (!empty($resultSet)) {
$queryString = $this->queryString;
$inputParams = $this->inputParams;
if (preg_match('/(.*)?LIMIT/is', $queryString, $match))
$queryString = $match[1];
$queryString = sprintf('SELECT COUNT(*) AS T FROM (%s) DT', $queryString);
if (($placeholders = substr_count($queryString, '?')) < count($inputParams))
$inputParams = array_slice($inputParams, 0, $placeholders);
if (($sth = $this->PDO->prepare($queryString)) && $sth->bindValues($inputParams) && $sth->execute())
$this->resultCount = $sth->fetchColumn();
$sth = null;
}
return $resultSet;
}
}
?>
nmurzin at mail dot ru ¶
1 year ago
I think I found a way to execute a protected SQL query and at the same time find out the number of affected records.
I have the table 'tbl_users' with the following fields: id, login, password, age
<?
const DB_DRIVER = "mysql";
const DB_HOST = "localhost";
const DB_NAME = "my_db_name";
const DB_LOGIN = "root";
const DB_PASS = "root"; //OpenServer.
$connectionString = DB_DRIVER.':host='.DB_HOST.';dbname='.DB_NAME;
try
{
//Connect to database.
$db = new PDO($connectionString, DB_LOGIN, DB_PASS);
}
catch(PDOException $e)
{
die("Error: ".$e->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
//Decreasing age for user 'nick1'.
$prep1 = $db->prepare("UPDATE tbl_users SET age=age-1 WHERE login='nick1'");
//Increasing age for user 'nick2'.
$prep2 = $db->prepare("UPDATE tbl_users SET \r\n age=age+1 WHERE login='nick2'");
//Start transaction.
$db->beginTransaction(); //Table type must be InnerDB!
//We assume that everything will be fine.
$flagDone = true;
//The exec() method returns the number of rows affected by the query.
//$prep1->queryString is already an escaped SQL string.
$result = $db->exec($prep1->queryString);
if($result==false || $result!=1) //var_dump($result) - int(1) or bool(false).
$flagDone = false;
$result = $db->exec($prep2->queryString);
if($result==false || $result!=1)
$flagDone = false;
if($flagDone)
{
if($db->commit())
echo "Transaction was successful";
}
else{
echo "Transaction fail";
$db->rollback();
}
echo "<br>";
}
catch(PDOException $e)
{
die("Error: ".$e->getMessage());
}