PDOStatement 类

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 1.0.0)

简介

表示预备语句,并在语句执行后,表示与之关联的结果集。

类概要

class PDOStatement implements IteratorAggregate {
/* 属性 */
/* 方法 */
public bindColumn(
    string|int $column,
    mixed &$var,
    int $type = PDO::PARAM_STR,
    int $maxLength = 0,
    mixed $driverOptions = null
): bool
public bindParam(
    string|int $param,
    mixed &$var,
    int $type = PDO::PARAM_STR,
    int $maxLength = 0,
    mixed $driverOptions = null
): bool
public bindValue(string|int $param, mixed $value, int $type = PDO::PARAM_STR): bool
public closeCursor(): bool
public columnCount(): int
public errorCode(): ?string
public errorInfo(): array
public execute(?array $params = null): bool
public fetch(int $mode = PDO::FETCH_DEFAULT, int $cursorOrientation = PDO::FETCH_ORI_NEXT, int $cursorOffset = 0): mixed
public fetchAll(int $mode = PDO::FETCH_DEFAULT): array
public fetchAll(int $mode = PDO::FETCH_COLUMN, int $column): array
public fetchAll(int $mode = PDO::FETCH_CLASS, string $class, ?array $constructorArgs): array
public fetchAll(int $mode = PDO::FETCH_FUNC, callable $callback): array
public fetchColumn(int $column = 0): mixed
public fetchObject(?string $class = "stdClass", array $constructorArgs = []): object|false
public getAttribute(int $name): mixed
public getColumnMeta(int $column): array|false
public nextRowset(): bool
public rowCount(): int
public setAttribute(int $attribute, mixed $value): bool
public setFetchMode(int $mode): bool
public setFetchMode(int $mode = PDO::FETCH_COLUMN, int $colno): bool
public setFetchMode(int $mode = PDO::FETCH_CLASS, string $class, ?array $constructorArgs = null): bool
public setFetchMode(int $mode = PDO::FETCH_INTO, object $object): bool
}

属性

queryString

使用的查询字符串。

变更日志

版本 描述
8.0.0 PDOStatement 现在实现了 IteratorAggregate 而不是 Traversable.

目录

添加注释

用户贡献的注释 2 notes

Gino D.
6 年前
我不知道为什么 PDOStatement 不返回“执行时间”和“找到的行”,所以在这里我创建了一个包含这些属性的 PDOStatement 扩展类。

只需将 PDO 对象的“setAttribute”设置为 $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
3 年前
我认为我找到了执行受保护的 SQL 查询并同时找出受影响记录数量的方法。
我有一个名为 'tbl_users' 的表,包含以下字段: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
{
//连接到数据库。
$db = new PDO($connectionString, DB_LOGIN, DB_PASS);
}
catch(PDOException $e)
{
die("错误: ".$e->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try
{

//减少用户 'nick1' 的年龄。
$prep1 = $db->prepare("UPDATE tbl_users SET age=age-1 WHERE login='nick1'");

//增加用户 'nick2' 的年龄。
$prep2 = $db->prepare("UPDATE tbl_users SET \r\n age=age+1 WHERE login='nick2'");

//开始事务。
$db->beginTransaction(); //表类型必须是 InnerDB!

//我们假设一切都会正常。
$flagDone = true;

//exec() 方法返回查询影响的行数。
//$prep1->queryString 已经是转义的 SQL 字符串。
$result = $db->exec($prep1->queryString);
if($result==false || $result!=1) //var_dump($result) - int(1) 或 bool(false)。
$flagDone = false;

$result = $db->exec($prep2->queryString);
if($result==false || $result!=1)
$flagDone = false;

if($flagDone)
{
if($db->commit())
echo "事务成功";
}
else{
echo "事务失败";
$db->rollback();
}
echo "<br>";

}
catch(PDOException $e)
{
die("错误: ".$e->getMessage());
}
To Top