|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-03-18 13:28 UTC] scottmac@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 19:00:01 2025 UTC |
Description: ------------ PDO method bindParam doesn't work properly in foreach cycle Reproduce code: --------------- <? $dbh = new PDO("sqlite:test.db3"); //1. CREATE TEST TABLE $create_table_query = "create table test (col1 varchar, col2 varchar, col3 varchar)"; if (!$sth = $dbh->prepare($create_table_query)) { print_r($dbh->errorInfo()); exit; } if ($sth->execute()) { echo "Test table created successfully\n"; } $sth = null; //2. INSERT ONE RECORD TO THE TEST TABLE $insert_record_query = "insert into test (col1,col2,col3) values (:col1, :col2, :col3)"; if (!$sth = $dbh->prepare($insert_record_query)) { print_r($dbh->errorInfo()); exit; } //3. DATA BINDINGS $bindings[":col1"] = "test value in col 1"; $bindings[":col2"] = "test value in col 2"; $bindings[":col3"] = "test value in col 3"; foreach ($bindings as $var => $value) { echo "Binding data, $var => $value\n"; $sth->bindParam($var, $value); } if ($sth->execute()) { echo "Record inserted successfully\n"; } $sth = null; //4. DISPLAY TABLE RECORD $show_record_query = "select * from test"; if (!$sth = $dbh->prepare($show_record_query)) { print_r($dbh->errorInfo()); exit; } if ($sth->execute()) { print_r($sth->fetchAll(PDO::FETCH_ASSOC)); } $dbh = null; ?> Expected result: ---------------- Test table created successfully Binding data, :col1 => test value in col 1 Binding data, :col2 => test value in col 2 Binding data, :col3 => test value in col 3 Record inserted successfully Array ( [0] => Array ( [col1] => test value in col 1 [col2] => test value in col 2 [col3] => test value in col 3 ) ) Actual result: -------------- Test table created successfully Binding data, :col1 => test value in col 1 Binding data, :col2 => test value in col 2 Binding data, :col3 => test value in col 3 Record inserted successfully Array ( [0] => Array ( [col1] => test value in col 3 [col2] => test value in col 3 [col3] => test value in col 3 ) )