Sometimes, we want to get raw SQL query string from PDO prepared statements with PHP.
In this article, we’ll look at how to get raw SQL query string from PDO prepared statements with PHP.
How to get raw SQL query string from PDO prepared statements with PHP?
To get raw SQL query string from PDO prepared statements with PHP, we can use the prepared statement object’s debugDumpParams
method.
For instance, we write
$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();
to call $dbh->prepare
to create a prepared statement object.
Then we call bindParam
and bindValue
to bind the values to the prepared statement placeholders.
And then we call execute
to run the statement.
And we use debugDumpParams
to dump the raw SQL query string.
Conclusion
To get raw SQL query string from PDO prepared statements with PHP, we can use the prepared statement object’s debugDumpParams
method.