PHP 8.4.2 Released!

PDOStatement::debugDumpParams

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

PDOStatement::debugDumpParams 打印一条 SQL 预处理命令

说明

public PDOStatement::debugDumpParams(): ?bool

直接打印出一条预处理语句包含的信息。提供正在使用的 SQL 查询、所用参数(Params)的数目、参数列表及其键名或位置、名称以及查询中的位置(如果当前 POD 驱动不支持,则为 -1),类型(param_type)为整数且 is_param 为布尔值。

这是调试函数,在正常输出的情况下直接输出数据。

小技巧

和直接将结果输出到浏览器一样,可使用输出控制函数来捕获当前函数的输出,然后(例如)保存到一个 string 中。

只打印此时此刻语句中的参数。额外的参数不存储在语句中,也就不会被输出。

参数

此函数没有参数。

返回值

返回 null, 或者错误时返回 false

更新日志

版本 说明
7.2.0 PDOStatement::debugDumpParams() 现在返回发送到数据库的 SQL,包括完整的原始查询(包括替换的占位符及其边界值)。请注意,这仅在打开模拟预处理语句时才可用。

示例

示例 #1 PDOStatement::debugDumpParams() 使用命名参数的示例

<?php
/* 通过绑定 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour'
);
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();

$sth->debugDumpParams();

?>

以上示例会输出:

SQL: [96] SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour
Params:  2
Key: Name: [9] :calories
paramno=-1
name=[9] ":calories"
is_param=1
param_type=1
Key: Name: [7] :colour
paramno=-1
name=[7] ":colour"
is_param=1
param_type=2

示例 #2 PDOStatement::debugDumpParams() 使用未命名参数的示例

<?php

/* 通过绑定 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$name = 'apple';

$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?'
);
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindValue(2, $colour, PDO::PARAM_STR);
$sth->execute();

$sth->debugDumpParams();

?>

以上示例会输出:

SQL: [82] SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?
Params:  2
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=1
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=2

参见

添加备注

用户贡献的备注 4 notes

up
70
thefox
11 years ago
This function doesn't print parameter values despite the documentation says it does. See https://bugs.php.net/bug.php?id=52384 (filed back in 2010).
up
29
mark at manngo dot net
11 years ago
As noted, this doesn’t actually simply print the prepared statement with data to be executed.

For trouble shooting purposes, I find the following useful:

<?php
function parms($string,$data) {
$indexed=$data==array_values($data);
foreach(
$data as $k=>$v) {
if(
is_string($v)) $v="'$v'";
if(
$indexed) $string=preg_replace('/\?/',$v,$string,1);
else
$string=str_replace(":$k",$v,$string);
}
return
$string;
}

// Index Parameters
$string='INSERT INTO stuff(name,value) VALUES (?,?)';
$data=array('Fred',23);

// Named Parameters
$string='INSERT INTO stuff(name,value) VALUES (:name,:value)';
$data=array('name'=>'Fred','value'=>23);

print
parms($string,$data);
?>
up
16
Lucas
9 years ago
This function doesn't have a return, so if you want to do something with it you'll have to do something like

<?php
function pdo_debugStrParams($stmt) {
ob_start();
$stmt->debugDumpParams();
$r = ob_get_contents();
ob_end_clean();
return
$r;
}

// omitted: connect to the database and prepare a statement
echo '<pre>'.htmlspecialchars(pdo_debugStrParams($stmt)).'</pre>';
?

Source: http://stackoverflow.com/questions/22157331/something-like-debugdumpparams-in-pdo-settable-to-a-string
up
0
wchasroth at gmail dot com
14 days ago
Using PHP 8.3 and MySQL 8.2, debugDumpParams() DOES display the "raw" SQL, with parameter values.

E.g.
$sql = "SELECT tid, miv_title, ballot_order FROM title WHERE tid LIKE :tid AND ballot_order = :order";
$stm = $pdo->prepare($sql);
$stm->bindValue(":tid", "mi:%");
$stm->bindValue(":order", 5000, PDO::PARAM_INT);
$stm->execute();
$stm->debugDumpParams();

... displays ...

SQL: [92] SELECT tid, miv_title, ballot_order FROM title WHERE tid LIKE :tid AND ballot_order = :order
Sent SQL: [92] SELECT tid, miv_title, ballot_order FROM title WHERE tid LIKE 'mi:%' AND ballot_order = 5000
Params: 2
Key: Name: [4] :tid
paramno=-1
name=[4] ":tid"
is_param=1
param_type=2
Key: Name: [6] :order
paramno=-1
name=[6] ":order"
is_param=1
param_type=1
To Top