PHP 8.3.4 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)、パラメータリスト、パラメータ名、 パラメータの型 (paramtype) をあらわす整数値、 そのキーの名前あるいは位置、そしてクエリ内での位置 (PDO ドライバがサポートしている場合のみ。サポートしていない場合は -1 となります) 、型 (param_type) を表す整数値、boolean 値 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

参考

add a note

User Contributed Notes 3 notes

up
68
thefox
10 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
10 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
18
Lucas
8 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
To Top