php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63139 PDO repeated use of prepared statement
Submitted: 2012-09-22 13:50 UTC Modified: 2013-01-12 05:20 UTC
From: davogotland at gmail dot com Assigned:
Status: Not a bug Package: Website problem
PHP Version: Irrelevant OS:
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: davogotland at gmail dot com
New email:
PHP Version: OS:

 

 [2012-09-22 13:50 UTC] davogotland at gmail dot com
Description:
------------
Hello!

I'm using PHP 5.2.5, so perhaps this is about my PHP version being old. But as I've noticed, changing whether or not a parameter needs to be sent by reference doesn't often happen.

The problem I've found is on this page:
  http://php.net/manual/en/pdo.prepared-statements.php

Under
  Example #1 Repeated inserts using prepared statements

There is some code that I have to modify to get to work.

  <?php
  $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
  $stmt->bindParam(':name', $name); // THIS
  $stmt->bindParam(':value', $value); // AND THIS
  
  //does not work for me. i have to do
  $stmt->bindParam(':name', &$name); // PLEASE NOTE
  $stmt->bindParam(':value', &$value); // THE AMPERSANDS
  
  
  // insert one row
  $name = 'one';
  $value = 1;
  $stmt->execute();
  
  // insert another row with different values
  $name = 'two';
  $value = 2;
  $stmt->execute();
  ?>


I also found this (from 2010) on the page
  http://php.net/manual/en/pdostatement.bindparam.php#98145

  This works ($val by reference):
  <?php
  foreach ($params as $key => &$val) {
      $sth->bindParam($key, $val);
  }
  ?>
  
  This will fail ($val by value, because bindParam needs &$variable):
  <?php
  foreach ($params as $key => $val) {
      $sth->bindParam($key, $val);
  }
  ?>

At the top of this last page, the version is declared as PHP 5 >= 5.1.0, which would include 5.2, 5.3 and so on. This is why I think this is a bug in the documentation.

Regards,
-David


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-01 20:12 UTC] JackDHunter26 at yahoo dot com
Try useing :Name1  and :Value1.  Likely it has problems seeing the difference between field names and varibles.
 [2013-01-12 05:20 UTC] frozenfire@php.net
This is not a bug. It's hard to nail down exactly what issue you might be 
incurring, but my suspicion is that your issue is due to the way you are getting 
each value from the array.

If you do not use the $key => &$value syntax, then it produces a copy of the 
value from the array, not a reference.

Call-time pass-by-reference is deprecated in PHP 5.3 and removed in PHP 5.4, so 
that's definitely not something to be added to the documentation.
 [2013-01-12 05:20 UTC] frozenfire@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 21:01:30 2024 UTC