Can any programming expert here explain to me why this snippet of PHP code results in an error:
code:
$db = require_once 'DB.php'; // returns a MySQLi connection object
$user = 'test';
$pass = sha1('test'); // Line 5
$prepared = $db->prepare("SELECT id FROM users WHERE user = ? AND pass = ? LIMIT 1");
$prepared->bind_param('ss', $user, $pass);
$prepared->execute();
$prepared->bind_result($id);
$prepared->fetch(); // $id now becomes 2. // Line 10
$prepared2 = $db->prepare("SELECT id, name, quote FROM metals");
$prepared2->execute();
?>
Error:
code:
Warning: mysqli::prepare() [function.mysqli-prepare]: All data must be fetched before a new statement prepare takes place in C:\...\test5.php on line 11
Fatal error: Call to a member function execute() on a non-object in C:\...\test5.php on line 12
Calling $prepared->close() after $prepared->fetch() prevents the error, but the thing is that as you can see from the "LIMIT 1" in the query, there is only 1 row of data to be fetched. So after the fetch() statement, I don't understand why should there be an error when the script tries to prepare another statement?
Here's something even weirder - if I call another fetch() statement after the first one, the error disappears as well. And through var_dump(), I found out that the first fetch() returns true and the second returns NULL - which means there is no data to be fetched already. And the value of $id doesn't change after the first fetch().
http://sg.php.net/manual/en/function.mysqli-stmt-fetch.phpAny idea what is actually going on behind all these issues?
PS: You guys wouldn't mind if I post this in other forums as well, right?