©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)
PDOStatement::debugDumpParams — 打印一条 SQL 预处理命令
直接打印出一条预处理语句包含的信息。提供正在使用的 SQL 查询、所用参数(Params)的数目、参数的清单、参数名、用一个整数表示的参数类型(paramtype)、键名或位置、值、以及在查询中的位置(如果当前 POD 驱动不支持,则为-1)。
此为一个用于调试的功能,在正常输出的情况下直接输出数据。
和直接将结果输出到浏览器一样,可使用输出控制函数来捕获当前函数的输出,然后(例如)保存到一个 string 中。
只打印此时此刻语句中的参数。额外的参数不存储在语句中,也就不会被输出。
没有返回值。
Example #1 PDOStatement::debugDumpParams() 使用命名参数的例子
<?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
Example #2 PDOStatement::debugDumpParams() 使用未命名参数的例子
<?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
[#1] mark at manngo dot net [2013-10-07 01:21:24]
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);
?>
[#2] thefox [2013-04-16 05:58:12]
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).
[#3] mattspopcorn at gmail dot com [2009-08-26 14:03:40]
It's not you, in PHP 5.2.6 and close versions this has an infinite loop bug. Upgrade to the latest php where it has been fixed.