Echo PDO query for debugging purposes

Asked
Active3 hr before
Viewed126 times

7 Answers

query
90%

Before moving to PDO, I created SQL queries in PHP by concatenating strings. If I got database syntax error, I could just echo the final SQL query string, try it myself on the database, and tweak it until I fixed the error, then put that back into the code.,Prepared PDO statements are faster and better and safer, but one thing bothers me: I never see the final query as it's sent to the database. When I get errors about the syntax in my Apache log or my custom log file (I log errors inside a catch block), I can't see the query that caused them.,A big thanks to bigwebguy and Mike from the StackOverflow thread Getting raw SQL query string from PDO for writing basically the entire main function behind this script. Big up!, Good question...this might explain it: stackoverflow.com/questions/9778887/…. Personally I used this function to debug Doctrine queries, and I think Doctrine uses numbered rather than named parameters so I didn't notice this issue. I updated the function so that it will work either with or without leading colons now. – Matt Browne Mar 30 '15 at 13:28

Sure you can debug using this mode {{ PDO::ATTR_ERRMODE }} Just add new line before your query then you will show the debug lines.

$db - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db - > query('SELECT *******');
load more v
88%

This is a debug function, which dumps the data directly to the normal output. ,PDO::prepare() - Prepares a statement for execution and returns a statement object, This will only dump the parameters in the statement at the moment of the dump. Extra parameters are not stored in the statement, and not displayed. ,As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a string (for example).

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
load more v
72%

The best thing you can do, for debugging purposes, is "re-construct" an "real" SQL query, by injecting the values into the SQL string of the statement.,Before moving to PDO, I created SQL queries in PHP by concatenating strings. If I got database syntax error, I could just echo the final SQL query string, try it myself on the database, and tweak it until I fixed the error, then put that back into the code.,I never see the final query as it's sent to the database,The database parses the query, and builds an internal representation of it

Sure you can debug using this mode {{ PDO::ATTR_ERRMODE }} Just add new line before your query then you will show the debug lines.

$db - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db - > query('SELECT *******');
load more v
65%

and use var_dump (or an equivalent) just after, to display the values of the parameters,echo the SQL code that corresponds to the statement, with placeholders,This is not great, when it comes to debugging -- but that's the price of prepared statements and the advantages they bring.,Probably what you want to do is use debugDumpParams() on the statement handle. You can run that any time after binding values to the prepared query (no need to execute() the statement).

load more v
75%

PDO sends the raw query (without real values) and the parameters independently to the database server. That’s it. So PHP will never know the full statement, as the final query is constructed by MySQL, not by PHP.,1. Let MySQL log the finally created values. This needs a lot of configuration and is definitly the best way to go, as this shows the real, final statements.,2. Take the raw SQL with the placeholders, take the parameter values and simply combine them in the way PDO would do. This is not perfect, as PDO does / allows more than just replacing the placeholders with real values (like changing types etc.). It’s just an emulation, and a very basic one.,A big thanks to bigwebguy and Mike on this StackOverflow thread: Getting raw SQL query string from PDO prepared statements. These guys have created and improved the basic function that is used in this free and open-source project. Send them a Thanks-notice if pdo-debug saves you a lot of time and nerves!

Your PDO block probably looks like this:

$sql = "INSERT INTO test (col1, col2, col3) VALUES (:col1, :col2, :col3)";
load more v
40%

The best thing you can do, for debugging purposes, is "re-construct" an "real" SQL query, by injecting the values into the SQL string of the statement.,Before moving to PDO, I created SQL queries in PHP by concatenating strings. If I got database syntax error, I could just echo the final SQL query string, try it myself on the database, and tweak it until I fixed the error, then put that back into the code.,I never see the final query as it's sent to the database,The database parses the query, and builds an internal representation of it

Sure you can debug using this mode {{ PDO::ATTR_ERRMODE }} Just add new line before your query then you will show the debug lines.

$db - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db - > query('SELECT *******');
load more v
22%

Turns out it was a simple if ($var="true") { ... which should have been if ($var=="true") { .... PHP I guess is not the same as Java in that sense? Either way, the question still stands (as I run into this often). I had to use a series of echo "You are Here"; to find this error, as it was technically valid but not correct. If I had the final SQL statement, I could have seen "Oh, my code has added the where column = true, must have gone through the wrong IF...".,I can't figure out why my query is returning 0 rows.. it implements some very dynamic search functionality, and a lot of if/loop statements etc. Therefor to debug it, I'd like to see EXACTLY what string is being sent to the server. Is there a way to do this through PHP?,Is there maybe a way to ask the server "what was the last query", or tell PDO "show me what you sent"?,I often see code using #code# or #code# with PDO. Is simply passing arguments to #code# frowned upon for any reason? I understand that #code# actually binds to...

To enable PDO Exceptions and disable emulated prepares:

$pdo = new PDO("mysql:host=localhost;dbname=database_name", "user", "password");
$pdo - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo - > setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Other "query-undefined" queries related to "Echo PDO query for debugging purposes"