note 116281 added to pdostatement.execute

From: Date: Fri, 05 Dec 2014 11:10:19 +0000
Subject: note 116281 added to pdostatement.execute
Groups: php.notes 
Request: Send a blank email to php-notes+get-201072@lists.php.net to get a copy of this message
Debugging prepared statements can be a pain sometimes when you need to copy a query and run it in the DB directly. The function below is an example of how to compile your own query (of course it would need some tweaking and may not work in all scenarios). <?php $sql = " SELECT t1.* FROM table1 AS t1 INNER JOIN table2 AS t2 ON ( t2.code = t1.code AND t1.field1 = ? AND t1.field2 = ? AND t1.field3 = ? ) "; $stmt = $pdo->prepare($sql); $params = [ 'A', 'B', 'C' ]; $stmt->execute($params); // Output the compiled query debug($sql, $params); function debug($statement, array $params = []) { $statement = preg_replace_callback( '/[?]/', function ($k) use ($params) { static $i = 0; return sprintf("'%s'", $params[$i++]); }, $statement ); echo '<pre>Query Debug:<br>', $statement, '</pre>'; } ?> This would output something like: SELECT t1.* FROM table1 AS t1 INNER JOIN table2 AS t2 ON ( t2.part_code = t1.code AND t1.field1 = 'A' AND t1.field2 = 'B' AND t1.field3 = 'C' ) ---- Server IP: 72.52.91.14 Probable Submitter: 188.39.149.82 ---- Manual Page -- http://php.net/manual/en/pdostatement.execute.php Edit -- https://master.php.net/note/edit/116281 Del: integrated -- https://master.php.net/note/delete/116281/integrated Del: useless -- https://master.php.net/note/delete/116281/useless Del: bad code -- https://master.php.net/note/delete/116281/bad+code Del: spam -- https://master.php.net/note/delete/116281/spam Del: non-english -- https://master.php.net/note/delete/116281/non-english Del: in docs -- https://master.php.net/note/delete/116281/in+docs Del: other reasons-- https://master.php.net/note/delete/116281 Reject -- https://master.php.net/note/reject/116281 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201072) next »