I have this really simple PHP code:
$mysqli = new mysqli('localhost', 'xxx', 'xxxxx', 'xxx');
$query = "SELECT * FROM questions WHERE id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('d', $_GET['qid']);
$stmt->execute();
$stmt->bind_result($id, $content, $correct_ans, $lol);
$stmt->fetch();
//do sth with the data
$query = "SELECT * FROM answers WHERE question_id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('d', $_GET['qid']);
$stmt->execute();
$stmt->bind_result($id, $content, $lol);
while($stmt->fetch())
{
//do sth
}
Basically, it seems that no matter what I do the second mysqli::prepare() will always return false, however mysqli::error is empty for some reason. Can anyone see a mistake here?
PS. It's not a duplicate question; yes, it has been already asked: MySQLi prepared statement returning false but the author didn't bother to share his solution with everybody.
EDIT: I thought I should explain this: ONLY the second prepare returns false, the first is absolutely fine. What is even more weird is that if I remove the first lines of code (ie the first prepare) the second will work without problems...
EDIT2: Well, all I can say now is WTF. It turns out that if I remove the assignment, (I don't do $stmt = prepare()...) but just call the function, $mysqli->error list is not empty - it says "Commands out of sync; you can't run this command now". If I do the assignment, it's empty...
EDIT3: I switched to PDO and it works perfectly. Until I can prove otherwise, I will assume that MySQLi is buggy.
Before second usage mysqli::prepare() you must either free mysqli result or close current mysqli statement:
...
//do sth with the data
$mysqli->free(); // or $stmt->close();
$query = "SELECT * FROM answers WHERE question_id = ?";
$stmt = $mysqli->prepare($query);
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With