php – How to get the prepare query string in PDO after processing

Question:

Hello!

I have a database query string:

SELECT * FROM `users` WHERE `id` = ?

I am using the PDO prepare function:

$sth = $dbh->prepare('SELECT * FROM `users` WHERE `id` = ?');

You need to get the query string after being processed by the prepare function, for example:

SELECT * FROM `users` WHERE `id` = 294

How to solve this problem?

Answer:

Use a library that allows you to do this

$parameters = array(
    'param1' => 'hello',
    'param2' => 123,
    'param3' => null
);

$sql = "INSERT INTO test (col1, col2, col3) VALUES (:param1, :param2, :param3)";
echo PdoDebugger::show($sql, $parameters);

//shows: INSERT INTO test (col1, col2, col3) VALUES ('hello', 123, NULL)
Scroll to Top