ID:               44468
 Updated by:       [EMAIL PROTECTED]
 Reported By:      petr at hroch dot info
-Status:           Open
+Status:           Bogus
 Bug Type:         PDO related
 Operating System: Linux Debian 4.0 Etch
 PHP Version:      5.3CVS-2008-03-18 (snap)
 New Comment:

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

You bind it to $value and when it exits the foreach then $value is
equal to "test value in col 3"

If you want it to behave as expected use.

foreach ($bindings as $var => &$value) {



Previous Comments:
------------------------------------------------------------------------

[2008-03-18 12:28:44] petr at hroch dot info

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
        )

)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=44468&edit=1

Reply via email to