mysqli出现问题怎么办?诸如 mysqli_fetch_array() 之类的错误:参数 #1 的类型必须是 mysqli_result

发布于 2025-01-19 21:24:27 字数 540 浏览 3 评论 0原文

在我的本地/开发环境中,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2025-01-26 21:24:27

TL;DR

  1. 在 mysqli 连接代码中始终包含 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);,并始终检查 PHP 错误。
  2. 始终将 SQL 查询中的每个 PHP 变量替换为问号,并使用准备好的语句。它将有助于避免各种语法错误。

说明

有时你的 mysqli 代码会产生一个错误,例如 mysqli_fetch_assoc() Expectsparameter 1 to be mysqli_result, booleangiven..., Call to a member function bind_param()...或类似的。或者即使没有任何错误,但查询仍然无法正常工作。这意味着您的查询无法执行。

每次查询失败时,MySQL 都会一条解释原因的错误消息。在旧的 PHP 版本中,此类错误不会转移到 PHP,您得到的只是上面提到的一条神秘的错误消息。因此,配置 PHP 和 mysqli 向您报告 MySQL 错误非常重要。一旦收到错误消息,您就能够修复该错误。

如何在 mysqli 中获取错误消息

首先,在您的所有环境中,在 mysqli 连接之前始终要有此行:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

之后,所有 MySQL 错误都将转换为 PHP 异常。未捕获的异常又会导致 PHP 致命错误。因此,如果出现 MySQL 错误,您将收到传统的 PHP 错误。这将立即让您意识到错误原因。堆栈跟踪将引导您找到发生错误的确切位置。

如何从 PHP 获取错误消息

在开发服务器上,确保错误显示在屏幕上,而在实时服务器上,请检查错误日志。有关详细信息,请参阅我关于 PHP 错误报告的文章。

请注意,如果是 AJAX 调用,请在开发服务器上打开 DevTools (F12),然后打开“网络”选项卡。然后发起您想要看到的结果的请求,它将出现在“网络”选项卡中。单击它,然后单击“响应”选项卡。在那里您将看到确切的输出。在实时服务器上检查错误日志。

如何处理收到的错误消息

首先,您必须找到问题查询。错误消息包含发生错误的确切位置的文件名和行号。对于简单的代码来说就足够了,但是如果您的代码使用函数或类,您可能需要按照堆栈跟踪来定位问题查询。

收到错误消息后,您必须阅读并理解它。这听起来太明显了,如果不是居高临下的话,但学习者经常忽略这样一个事实:错误消息不仅仅是一个警报信号,而且它实际上包含了问题的详细解释。您所需要做的就是阅读错误消息并解决问题。

  • 比如说,如果它说某个特定的表不存在,您必须检查拼写、拼写错误和字母大小写。另外,您还必须确保您的 PHP 脚本连接到正确的数据库
  • ,或者,如果它说 SQL 语法有错误,那么您必须检查您的 SQL。问题点就在错误消息中引用的查询部分之前

如果您不明白错误消息,请尝试用 google 搜索。在浏览结果时,请坚持使用解释错误的答案,而不是直白地给出解决方案。解决方案可能不适用于您的特定情况,但解释将帮助您理解问题并使您能够自己解决问题。

您还必须信任错误消息。如果它说标记的数量与绑定变量的数量不匹配,那么它就是如此。对于缺失的表或列也是如此。如果有选择,无论是你自己的错误还是错误消息是错误的,请始终坚持前者。这听起来又有点居高临下,但这个网站上的数百个问题证明这个建议非常有用。

基本调试

如果您的查询看似不起作用,则可能是由以下四个原因引起的:

  1. 执行期间出现错误。上面已经解释过了。
  2. 由于程序逻辑不正确,SQL 根本没有运行。添加临时调试输出以确保代码到达查询执行的位置。
  3. SQL执行成功,但在错误的数据库中查看结果。仔细检查
  4. 输入数据与数据库不匹配。以下是关于如何检查的一些建议

TL;DR

  1. Always have mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); in your mysqli connection code and always check the PHP errors.
  2. Always replace every PHP variable in the SQL query with a question mark, and execute the query using prepared statement. It will help to avoid syntax errors of all sorts.

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:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

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.

  • Say, if it says that a particular table doesn't exist, you have to check spelling, typos, and letter case. Also you have to make sure that your PHP script connects to a correct database
  • Or, if it says there is an error in the SQL syntax, then you have to examine your SQL. And the problem spot is right before the query part cited in the error message.

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:

  1. There was an error during execution. Already explained above.
  2. The SQL didn't run at all due to incorrect program logic. Add temporary debugging output to make sure that the code reached the point where the query executes.
  3. The SQL was executed successfully but the result is viewed in the wrong database. Double check it
  4. The input data doesn't match that the database. Here is some recommendations on how to check it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文