|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-09-17 22:26 UTC] evangun2001 at yahoo dot fr
Description:
------------
Hello,
As you can see in the code below, binding to $value['id'] will not work because $value is a variable generated by the foreach loop. Using a dimensional variable as a parameter causes no bugs outside a foreach loop, so I'm pretty sure it really is a bug.
$arr = array(array('id' => 1));
$stmt = $dbh -> prepare('SELECT member_login FROM members WHERE member_id = ?;');
$stmt -> bind_param('i', $value['id']);
foreach($arr as $value){
echo $value['id']; //this will output 1 as expected
$stmt -> execute();
$stmt -> bind_result($login);
while($stmt -> fetch()){
echo $login; //this will output nothing
}
}
Reproduce code:
---------------
The code I've given in description pretty much says it all !
For a quick and global understanding of the situation, I've also put a summary of what works, what does not work, and the workaround I've been using so far. Check it here :
http://trombiaudencia2.free.fr/bug.txt
Actual result:
--------------
Just to let you know, in my application, the real bind_param line looked like :
$stmt -> bind_param('si', $_POST['members'][$value['id']]['login'], $value['id']);
and in that case, php told me :
Fatal error: Only variables can be passed by reference in E:\Program Files\EasyPHP\www\create_quiz_ajaxrequests.php on line 368
which is due to the second parameter, the first one does not create any warning at all. Strange, because you'll see that in the simplified example I've given above, no error message is displayed at all.
I use loops all the time so to me, it's reeaaally a down side for MySQLi not to be able to bind parameters. I have been using PDO for a while and heard MySQLi had better performances, but this kinda cools me down :( Maybe I'll wait a bit until this bug is fixed before I migrate to MySQLi.
Thank you very much !!
Evangun
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 19:00:02 2025 UTC |
Bogus issue. In the test case you have this $stmt -> bind_param('i', $value['id']); foreach($arr as $value){ echo $value['id']; //this will output 1 as expected Which is equivalent to the following example : andrey@whirlpool:~/dev/vanilla/php5_3> ./php -r ' $value["id"] = 2; var_dump($value); $arr = array(1, 2, 3); var_dump($arr); foreach ($arr as $value) { var_dump($value); } var_dump($value);' array(1) { ["id"]=> int(2) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) int(2) int(3) int(3) ---------------- What you do is you overwrite $value, the fetch works in the background and fetches in what $value['id'] used to be (just a NULL zval). However, your foreach uses $value for iterating the values, thus you get 1 for the first (and last) iteration.