Bug #63281 [Wfx]: PDO: Multiple queries using multiple bindParams yields unexpected results

From: Date: Tue, 16 Oct 2012 15:08:29 +0000
Subject: Bug #63281 [Wfx]: PDO: Multiple queries using multiple bindParams yields unexpected results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-174102@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63281&edit=1 ID: 63281 Updated by: laruence@php.net Reported by: jim dot gibbs at onelifemedia dot com Summary: PDO: Multiple queries using multiple bindParams yields unexpected results Status: Wont fix Type: Bug Package: PDO related Operating System: Mac PHP Version: 5.4.7 Block user comment: N Private report: N New Comment: I wrote a blog to explain this: http://www.laruence.com/2012/10/16/2831.html it's chinese, but I think you can use google translator :) Previous Comments: ------------------------------------------------------------------------ [2012-10-16 14:42:19] laruence@php.net the reason for this is because require a reference variable, then in the loop, the variable will be reused, after the loop, the variable will be the last assigned one. you can use bindValue instead, or ... you can use: foreach ($params as $key => &$val) ; this is a complicated problem, since it is a feature of PHP internal(reference). so, won't fix. thanks ------------------------------------------------------------------------ [2012-10-15 18:18:17] jim dot gibbs at onelifemedia dot com Description: ------------ Created a prepared statement, with named placeholders. The prepared statement contained 2 queries, and 3 placeholders. 2 of the placeholders were the same, so they *should* become the same value. The resulting query ended up being all three place holders being the same value. The results below were generated from the mysql log files. Workaround: Instead of using the bindParam function, I was able to send the same array to the execute function, and it worked. Test script: --------------- $query = <<<QUERY DELETE FROM authentication_hashes WHERE session_key = :session_key; INSERT INTO authentication_hashes (session_key, generated_hash) VALUES (:session_key, :generated_hash); QUERY; $dbh = new PDO('mysql:host=localhost;port=8888;dbname=foo, $user, $pass, array (PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); $statement = $dbh->prepare($query); $bind_params = array(':session_key' => $session_key, ':generated_hash' => $generated_hash) foreach( $bind_params as $key => $value ){ $statement->bindParam($key, $value); } $statement->execute(); Expected result: ---------------- DELETE FROM authentication_hashes WHERE session_key = '8675309'; INSERT INTO authentication_hashes (session_key, generated_hash) VALUES ('8675309', 'cb5606644c1d30a9d8f84cee4234a44455c82448a33f8031434ec42a6d173383') Actual result: -------------- DELETE FROM authentication_hashes WHERE session_key = '3966b45ae07b86de9c74163b093994fbf2a5813a06cbc9902f15e1b38a212940'; INSERT INTO authentication_hashes (session_key, generated_hash) VALUES ('3966b45ae07b86de9c74163b093994fbf2a5813a06cbc9902f15e1b38a212940', '3966b45ae07b86de9c74163b093994fbf2a5813a06cbc9902f15e1b38a212940') ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63281&edit=1

« previous php.bugs (#174102) next »