如何处理mysqli问题?诸如mysqli_fetch_array()之类的错误:参数#1必须是类型mysqli_result
在我的本地/开发环境中,MySQLI查询的性能正常。但是,当我将其上传到我的Web主机环境中时,我会收到此错误:
致命错误:在...
中的非对象上调用成员函数bind_param()
这是...这是代码:
global $mysqli;
$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");
$stmt->bind_param('i', $cur_id);
$stmt->execute();
$stmt->bind_result($uid, $desc);
要检查我的查询,我尝试通过控制面板 phpmyadmin ,结果还可以。
In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error:
Fatal error: Call to a member function bind_param() on a non-object in...
Here is the code:
global $mysqli;
$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");
$stmt->bind_param('i', $cur_id);
$stmt->execute();
$stmt->bind_result($uid, $desc);
To check my query, I tried to execute the query via control panel phpMyAdmin and the result is OK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tl; dr
mysqli_report(mysqli_report_error | mysqli_report_strict_strict);
在您的mysqli连接代码中,并始终检查php errors 。说明
有时您的mysqli代码会产生一个错误,例如
mysqli_fetch_assoc()期望参数1为mysqli_result,boolean给出了...
,调用成员函数bind_param()...
或类似。甚至没有任何错误,但是查询无法正常工作。这意味着您的查询无法执行。每次查询失败时,MySQL都会有一个错误消息,解释了原因。在较旧的PHP版本中,此类错误未转移到PHP,而您所获得的只是上面提到的一个隐秘错误消息。因此,配置PHP和MySQLI向您报告MySQL错误非常重要。一旦收到错误消息,您将能够解决错误。
如何
首先在mysqli中获取错误消息,在 your 您的环境中连接mysqli之前,请始终具有此行:
在此之后,所有mySQL错误都将转移到PHP例外。一个未知的例外又导致了PHP致命错误。因此,如果发生MySQL错误,您将获得常规的PHP错误。这将立即使您意识到错误原因。并且堆栈跟踪将使您进入发生错误的确切位置。
如何从
开发服务器上的PHP获取错误消息,请确保在屏幕上显示错误,而在实时服务器上检查错误日志。请参阅我的文章 php错误报告有关详细信息。
请注意,如果在AJAX呼叫的情况下,在开发服务器打开DevTools(F12)上,则是网络选项卡。然后启动您要查看的结果的请求,并将显示在“网络”选项卡中。单击它,然后单击响应选项卡。在那里您会看到确切的输出。在实时服务器上检查错误日志。
如何处理错误消息,
您首先要找到问题查询。错误消息包含 发生错误的确切位置的线号。对于足够的简单代码,但是如果您的代码正在使用函数或类,则可能需要遵循 stack跟踪以找到问题查询。
收到错误消息后,您必须阅读并理解它。听起来很明显,如果不屈服,学习者经常忽略以下事实:错误消息不仅是警报信号,而且实际上包含了问题的详细说明。您需要的只是阅读错误消息并解决问题。
如果您不了解错误消息,请尝试使用Google。并且在浏览结果时,请坚持解释的答案 而不是直截了当地提供解决方案。解决方案可能在您的特定情况下不起作用,但是解释将帮助您理解问题并使您能够自己解决问题。
您还必须 trust 错误消息。如果说令牌的数量与界变量的数量不匹配,则它 is so。缺席的表或列也是如此。考虑到选择,无论是您自己的错误还是错误消息是错误的,请始终坚持前者。同样,这听起来很屈服,但是这个网站上的数百个问题证明了这一点非常有用。
基本调试
如果您的查询似乎不起作用,则可能是由以下四个原因引起的:
TL;DR
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
in your mysqli connection code and always check the PHP errors.Explanation
Sometimes your mysqli code produces an error like
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given...
,Call to a member function bind_param()...
or similar. Or even without any error, but the query doesn't work all the same. It means that your query failed to execute.Every time a query fails, MySQL has an error message that explains the reason. In the older PHP versions such errors weren't transferred to PHP, and all you'd get is a cryptic error message mentioned above. Hence it is very important to configure PHP and mysqli to report MySQL errors to you. And once you get the error message, you will be able to fix the error.
How to get the error message in mysqli
First of all, always have this line before mysqli connect in all your environments:
After that, all MySQL errors will be transferred into PHP exceptions. An uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you'll get a conventional PHP error. That will instantly make you aware of the error cause. And the stack trace will lead you to the exact spot where the error occurred.
How to get the error message from PHP
On the development server make sure that errors are shown on-screen, while on a live server check the error logs. See my article on PHP error reporting for the details.
Note that in case of AJAX call, on a dev server open DevTools (F12), then Network tab. Then initiate the request which result you want to see, and it will appear in the Network tab. Click on it and then the Response tab. There you will see the exact output. On a live server check the error log.
What to do with the error message you get
First of all you have to locate the problem query. The error message contains the file name and the line number of the exact spot where the error occurred. For the simple code that's enough, but if your code is using functions or classes you may need to follow the stack trace to locate the problem query.
After getting the error message, you have to read and comprehend it. It sounds too obvious if not condescending, but learners often overlook the fact that the error message is not just an alarm signal, but it actually contains a detailed explanation of the problem. And all you need is to read the error message and fix the issue.
If you don't understand the error message, try to google it. And when browsing the results, stick to answers that explain the error rather than bluntly give the solution. A solution may not work in your particular case, but the explanation will help you to understand the problem and make you able to fix the issue by yourself.
You have to also trust the error message. If it says that number of tokens doesn't match the number of bound variables then it is so. The same goes for the absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.
Basic debugging
In case your query seemingly doesn't work, it can be caused by four following reasons: