PHP - 的mysqli :: prepare返回false [英] PHP - mysqli::prepare returning false

查看:1967
本文介绍了PHP - 的mysqli :: prepare返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个非常简单的PHP code:

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
}

基本上,它似乎无论我做什么,第二个的mysqli :: prepare()将始终返回false,但的mysqli ::误差为空,出于某种原因。任何人都可以在这里看到一个错误?

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。这不是一个重复的问题;是的,它已经被问: MySQLi的prepared语句返回false 但作者并没有刻意去分享他与大家的解决方案。

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.

编辑:我想我应该解释:只有第二prepare返回false,首先是精美绝伦。更令人奇怪的是,如果我删除code的第一线(即第一个prepare),第二个将不工作的问题...

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:嗯,我能说的是现在跆拳道。事实证明,如果我删除的任务,(我不做 $语句= prepare()...)只是调用函数, $ mysqli->错误列表不为空 - 它说:命令不同步,你现在不能运行此命令。如果我做的任务,它是空的......

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:我切换到PDO和它完美的作品。直到我可以证明,否则,我会认为是库MySQLi马车。

I switched to PDO and it works perfectly. Until I can prove otherwise, I will assume that MySQLi is buggy.

推荐答案

第二次使用前的mysqli :: prepare()您必须的免费的mysqli导致或的关闭当前的mysqli声明

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);
...

这篇关于PHP - 的mysqli :: prepare返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆