ID:               47977
 Updated by:       u...@php.net
 Reported By:      fhgvbrdyftgjhgtfr at gazeta dot pl
-Status:           Open
+Status:           Bogus
 Bug Type:         PDO related
 Operating System: Unix
 PHP Version:      5.2.9
 New Comment:

"Binds a PHP variable to a corresponding named or question mark
placeholder in the SQL statement that was use to prepare the statement.
Unlike PDOStatement::bindValue(), the variable is bound as a reference
and will only be evaluated at the time that PDOStatement::execute() is
called.", http://de2.php.net/manual/en/pdostatement.bindparam.php

At the time that you call PDOStatement::execute() you have overwritten
the bound variables' value ($element) multi times. PDO does exactly what
is documented: it uses the last value of $element. 

Workaround: use bindValue() instead of bindParam().






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

[2009-09-27 19:54:45] fhgvbrdyftgjhgtfr at gazeta dot pl

<?php
$db = new PDO('mysql:dbname=test;host=localhost', 'root', '');
$array2 = array('fr', 'gb', 'de');
$array3 = array('de'=>true);
$array = array_diff($array2, array_keys($array3));
reset($array);
$stmt = $db->prepare('SELECT `name` FROM `table` WHERE `language` = ?
AND `code` IN(?, ?)');
$stmt->bindValue(1, 'en', PDO::PARAM_STR);
$element = current($array);
$stmt->bindParam(2, $element, PDO::PARAM_STR);
$element = next($array);
$stmt->bindParam(3, $element, PDO::PARAM_STR);
$stmt->execute();
var_dump($stmt->fetchAll());
?>
---------------
actual result:
array(1) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "test"
    [0]=>
    string(4) "test"
  }
}
---------------
expected result:
array(2) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "test"
    [0]=>
    string(4) "test"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(4) "test"
    [0]=>
    string(4) "test"
  }
}
---------------
db:
CREATE TABLE `table` (
  `name` varchar(45) NOT NULL,
  `language` enum('en','fr') NOT NULL,
  `code` char(2) NOT NULL
);
INSERT INTO `table` (`name`, `language`, `code`) VALUES
('test', 'en', 'fr'),
('test', 'en', 'gb');
---------------
if you change

$stmt->bindParam(2, $element, PDO::PARAM_STR);
for
$stmt->bindValue(2, $element, PDO::PARAM_STR);

AND

$stmt->bindParam(3, $element, PDO::PARAM_STR);
for
$stmt->bindValue(3, $element, PDO::PARAM_STR);

you will receive expected result

i can't write it easier, so deal with it or just delete this report.

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

[2009-09-02 01:00:00] php-bugs at lists dot php dot net

No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".

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

[2009-08-25 17:52:38] sjo...@php.net

Thank you for your bug report.

Although you supplied code to reproduce the problem, it is more
complicated and longer than necessary. Please supply a short, complete
and understandable piece of code to reproduce the problem. This is to
make sure the problem is with PHP and not with the example.

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

[2009-05-16 01:20:50] fhgvbrdyftgjhgtfr at gazeta dot pl

<?php
$db = new PDO('mysql:dbname=test;host=localhost', 'root', '');
$array2 = array('gb', 'us', 'nz', 'fr', 'de');
$array3 = array('fr'=>true, 'de'=>true);
$array = array_diff($array2, array_keys($array3));
reset($array);
$size = count($array);
$in = '?';
for($i = 1; $i < $size; ++$i) $in .= ', ?';
$stmt = $db->prepare('SELECT `name` FROM `table` WHERE `language` = ?
AND `code` IN('.$in.')');
$stmt->bindValue(1, 'en', PDO::PARAM_STR);
$i = 1;
$element = current($array);
do {
        $stmt->bindParam(++$i, $element, PDO::PARAM_STR);
} while(($element = next($array)) !== false);
$stmt->execute();
var_dump($stmt->fetchAll());
?>
-----------------
expected result:
array(3) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "test"
    [0]=>
    string(4) "test"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(4) "test"
    [0]=>
    string(4) "test"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(4) "test"
    [0]=>
    string(4) "test"
  }
}
--------------------
actual result:
array(0) {
}
--------------------
db:
CREATE TABLE IF NOT EXISTS `table` (
  `name` varchar(45) NOT NULL,
  `language` enum('en','fr') NOT NULL,
  `code` char(2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


INSERT INTO `table` (`name`, `language`, `code`) VALUES
('test', 'en', 'gb'),
('test', 'en', 'nz'),
('test', 'en', 'us'),
('test', 'en', 'cz');

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

[2009-05-08 01:00:02] php-bugs at lists dot php dot net

No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".

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

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/47977

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

Reply via email to