php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44468 PDO SQlite method bindParam doesn't work properly in foreach cycle
Submitted: 2008-03-18 12:28 UTC Modified: 2008-03-18 13:28 UTC
From: petr at hroch dot info Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.3CVS-2008-03-18 (snap) OS: Linux Debian 4.0 Etch
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: petr at hroch dot info
New email:
PHP Version: OS:

 

 [2008-03-18 12:28 UTC] 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
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-18 13:28 UTC] scottmac@php.net
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) {

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 14:01:36 2024 UTC