Edit report at https://bugs.php.net/bug.php?id=62207&edit=1
ID: 62207 Updated by: johan...@php.net Reported by: mike dot mackintosh at angrystatic dot com Summary: foreaching bindparam results in all params equaling last value -Status: Open +Status: Not a bug Type: Bug Package: PDO related Operating System: Ubuntu 12.04 x86_64 PHP Version: 5.4.4RC2 Block user comment: N Private report: N New Comment: As said above. Previous Comments: ------------------------------------------------------------------------ [2012-06-01 19:51:48] mail+php at requinix dot net Er, when you call execute(), not query(). ------------------------------------------------------------------------ [2012-06-01 19:49:07] mail+php at requinix dot net Not a bug. bindParam() works the value by-reference, which means you passed it a reference to the $value variable. Not its value at the time you called it. When the loop ends and you call query(), $value will be whatever the last one in the $array was: 1. Use a reference to the item in the array: $stmt2->bindParam($param, $array[$param]); ------------------------------------------------------------------------ [2012-06-01 14:39:11] mike dot mackintosh at angrystatic dot com Description: ------------ I have noticed an issue when working with MySQL and PDO prepare statements. If running a foreach on an array which contains the params for the prepare statement, all params equal the value of the last param in the array. See below. Test script: --------------- $pdo = new PDO('mysql:dbname=netsec_v2;host=localhost;', 'root', ''); $stmt = $pdo->prepare("CREATE TABLE BugTest ( ID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), ColumnA varchar(15) NOT NULL, ColumnB int NOT NULL )"); $stmt->execute(); $array = array(":ColumnA" => "A", ":ColumnB" => 1); $stmt2 = $pdo->prepare("INSERT INTO BugTest (ColumnA,ColumnB) VALUES (:ColumnA, :ColumnB)"); foreach($array as $param => $value){ $stmt2->bindParam($param, $value); } $stmt2->execute(); $res = $pdo->query("SELECT * FROM BugTest")->fetchAll(PDO::FETCH_ASSOC); var_dump($res); Expected result: ---------------- ColumnA = A ColumnB = 1 Actual result: -------------- ColumnA = 1 ColumnB = 1 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62207&edit=1