I previously submitted an RFC which was accepted and implemented. I had second thoughts about the implementation and would like to replace it with the approach described here. Please see the original RFC for a description of the underlying problem.
PDO already provides some debug functionality in the form of PDOStatement::debugDumpParams()
. This method would be extended to include the parsed query string:
/* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $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(); /* Output: SQL: [82] SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ? Sent SQL: [88] SELECT name, colour, calories FROM fruit WHERE calories < 150 AND colour = 'red' 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 */
The “Sent SQL” section will only be shown after a query is executed, if prepared statement emulation is enabled. This would allow this information to be accessed without any API changes. If prepared statement emulation is removed, as some have suggested it should be, this feature would vanish seamlessly.
The original RFC will be released with PHP 7.2. If this RFC is accepted in time for that release, there will be no BC breaks.
Next PHP 7.x.
This project requires a 50%+1 majority. Voting opened on December 7th. It will end on 16 December 2016 at 0:00 UTC.
A working implementation with tests: https://github.com/adambaratz/php-src/commit/a030d2f4e6a13f3e6c10484fb5dbc4e8be6576c6
Discussion on the internals mailing list: http://marc.info/?l=php-internals&m=148034706404511&w=2
This feature was implemented in PHP 7.2 (d58231dda300b015260ea8fb17c28f8eebf1ec51).