note 114974 added to book.pdo

From: Date: Wed, 07 May 2014 23:31:26 +0000
Subject: note 114974 added to book.pdo
Groups: php.notes 
Request: Send a blank email to php-notes+get-199482@lists.php.net to get a copy of this message
When using prepared statements there is no official PDO feature to show you the final query string that is submitted to a database complete with the parameters you passed. Use this simple function for debugging. The values you are passing may not be what you expect. <?php //Sample query string $query = "UPDATE users SET name = :user_name WHERE id = :user_id"; //Sample parameters $params = [':user_name' => 'foobear', ':user_id' => 1001]; function build_pdo_query($string, $array) { //Get the key lengths for each of the array elements. $keys = array_map('strlen', array_keys($array)); //Sort the array by string length so the longest strings are replaced first. array_multisort($keys, SORT_DESC, $array); foreach($array as $k => $v) { //Quote non-numeric values. $replacement = is_numeric($v) ? $v : "'{$v}'"; //Replace the needle. $string = str_replace($k, $replacement, $string); } return $string; } echo build_pdo_query($query, $params); //UPDATE users SET name = 'foobear' WHERE id = 1001 ?> ---- Server IP: 72.52.91.14 Probable Submitter: 205.254.247.11 ---- Manual Page -- http://php.net/manual/en/book.pdo.php Edit -- https://master.php.net/note/edit/114974 Del: integrated -- https://master.php.net/note/delete/114974/integrated Del: useless -- https://master.php.net/note/delete/114974/useless Del: bad code -- https://master.php.net/note/delete/114974/bad+code Del: spam -- https://master.php.net/note/delete/114974/spam Del: non-english -- https://master.php.net/note/delete/114974/non-english Del: in docs -- https://master.php.net/note/delete/114974/in+docs Del: other reasons-- https://master.php.net/note/delete/114974 Reject -- https://master.php.net/note/reject/114974 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199482) next »