PDOStatement::debugDumpParams

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

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

说明

PDOStatement::debugDumpParams ( void ) : bool

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

此为一个用于调试的功能,在正常输出的情况下直接输出数据。

Tip

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

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

返回值

没有返回值。

范例

Example #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'$caloriesPDO::PARAM_INT);
$sth->bindValue(':colour'$colourPDO::PARAM_STR12);
$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

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

$sth $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?'
);
$sth->bindParam(1$caloriesPDO::PARAM_INT);
$sth->bindValue(2$colourPDO::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

参见

User Contributed Notes

Lucas 15-Dec-2015 02:15
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
mark at manngo dot net 07-Oct-2013 01:21
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);
?>
thefox 16-Apr-2013 05:58
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).
mattspopcorn at gmail dot com 26-Aug-2009 02:03
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.