Debug PDO prepared queries using MySQL

I am making a change from calls mysql_*to using PDO calls and cannot figure out how to debug my actual SQL when developing new code.

In mysql_*calls, I could write some SQL in a PHP function, and I could echouse the actual SQL to see what the processor was working with.

I could not find such a star in the PDO library. debugDumpParamsIt looks as it should, but does not return a back-bound statement.

Examples of problems I encountered:

  • In my first attempt, I anchored a string and included quotation marks in the SQL statement, despite binding to string_type of the string - it was only by accident that I tried to remove quotes from the statement; debugging would let me see that there was a repeated quote.

  • I copied the code from one project to another and accidentally forgot to fix the database name. Naturally, SQL failed because the tables did not exist in another database. But the program simply returned the correct false result. Nowhere in PHP logs, nor in MySQL logs, nor anywhere did I realize that the table does not exist where I searched for it.

So how do other people debug for SQL PDO calls? What am I missing? :)

+5
source share
1 answer

. , . , () . , . , $yourStatement->queryString. .

, PDO . , . . http://php.net/manual/en/pdo.error-handling.php

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try/catch .

+4

All Articles