mysqli_fetch_assoc() 期望参数/调用成员函数 bind_param() 错误.如何获取实际的 mysql 错误并修复它? [英] mysqli_fetch_assoc() expects parameter / Call to a member function bind_param() errors. How to get the actual mysql error and fix it?

查看:17
本文介绍了mysqli_fetch_assoc() 期望参数/调用成员函数 bind_param() 错误.如何获取实际的 mysql 错误并修复它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的本地/开发环境中,MySQLi 查询执行正常.但是,当我将其上传到我的 Web 主机环境时,出现此错误:

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:

致命错误:在...中的非对象上调用成员函数 bind_param()

Fatal error: Call to a member function bind_param() on a non-object in...

代码如下:

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 执行查询,结果正常.

To check my query, I tried to execute the query via control panel phpMyAdmin and the result is OK.

推荐答案

有时你的 MySQLi 代码会产生一个错误,比如 mysqli_fetch_assoc() expects parameter..., Call to a member function bind_param()... 或类似的.或者甚至没有任何错误,但查询并不完全相同.这意味着您的查询未能执行.

Sometimes your MySQLi code produces an error like mysqli_fetch_assoc() expects parameter..., 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.

每次查询失败时,MySQL 都会一条解释原因的错误消息.不幸的是,默认情况下,此类错误不会转移到 PHP,您所得到的只是上面提到的神秘错误消息.因此,配置 PHP 和 MySQLi 以向您报告 MySQL 错误非常重要.一旦您收到错误消息,修复它就会变得轻而易举.

Every time a query fails, MySQL has an error message that explains the reason. Unfortunately, by default such errors are not transferred to PHP, and all you've got 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, fixing it will be a piece of cake.

首先,在 MySQLi 连接所有你的环境之前,总是有这条线:

First of all, always have this line before MySQLi connect in all your environments:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

之后所有的 MySQL 错误都会被转移到 PHP 异常中.反过来,未捕获的异常会导致 PHP 致命错误.因此,如果出现 MySQL 错误,您将收到常规的 PHP 错误.这将立即让您知道错误原因.堆栈跟踪将引导您到发生错误的确切位置.

After that all MySQL errors will be transferred into PHP exceptions. 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 a stack trace will lead you to the exact spot where the error occurred.

这是我关于 PHP 错误报告的文章的要点:
在开发服务器和实时服务器上报告错误必须不同.在开发服务器上,在屏幕上显示错误很方便,但在实时服务器上必须记录错误消息,以便您稍后可以在错误日志中找到它们.

Here is a gist of my article on PHP error reporting:
Reporting errors on a development and live servers must be different. On a development server it is convenient to have errors shown on-screen, but on a live server error messages must be logged instead, so you could find them in the error log later.

因此,您必须将相应的配置选项设置为以下值:

Therefore, you must set corresponding configuration options to the following values:

  • 在开发服务器上

  • On a development server

  • error_reporting 应设置为 E_ALL 值;
  • log_errors 应该设置为 1(在开发 PC 上也有日志很方便)
  • display_errors 应该设置为 1
  • error_reporting should be set to E_ALL value;
  • log_errors should be set to 1 (it is convenient to have logs on a development PC too)
  • display_errors should be set to 1

在生产服务器上

  • error_reporting 应设置为 E_ALL 值;
  • log_errors 应该设置为 1
  • display_errors 应该设置为 0
  • error_reporting should be set to E_ALL value;
  • log_errors should be set to 1
  • display_errors should be set to 0

只需删除任何手动检查错误的代码,所有那些or die()if ($result) 等等.只需立即编写您的数据库交互代码:

Just remove any code that checks for the error manually, all those or die(), if ($result) and such. Simply write your database interaction code right away:

$stmt = $this->con->prepare("INSERT INTO table(name, quantity) VALUES (?,?)");
$stmt->bind_param("si", $name, $quantity);
$stmt->execute();

再次,没有任何条件.如果发生错误,它将被视为代码中的任何其他错误.例如,在开发 PC 上,它只会出现在屏幕上,而在实时站点上,它将为程序员记录,而为了用户的方便,您可以使用错误处理程序(但这是一个不同的故事,它与主题无关)MySQLi,但您可以在上面链接的文章中阅读它).

again, without any conditions around. If an error occurs, it will be treated as any other error in your code. For example, on a development PC it will just appear on-screen, while on a live site it will be logged for a programmer, whereas for the user's convenience you could use an error handler (but that's a different story which is off topic for MySQLi, but you may read about it in the article linked above).

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

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.

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

如果您不明白错误信息,请尝试谷歌.浏览结果时,请坚持解释错误的答案,而不是直截了当地给出解决方案.解决方案可能不适用于您的特定情况,但解释将帮助您理解问题并使您能够自行解决问题.

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.

  • 切勿使用错误抑制运算符 (@)!它使程序员无法读取错误消息,因此无法修复错误
  • 不要使用 die()echo 或任何其他函数无条件地在屏幕上打印错误信息.PHP 可以自行报告错误,并根据环境以正确的方式报告错误 - 所以将其留给 PHP.
  • 不要添加条件来手动测试查询结果(如if($result)).启用错误异常后,这种情况将毫无用处.
  • 不要使用 try..catch 运算符来回显错误消息.这个操作符应该用于执行一些错误处理,比如事务回滚.但不要仅将其用于报告错误 - 正如我们在上面了解到的,PHP 已经可以以正确的方式做到这一点.
  • Never use an error suppression operator (@)! It makes a programmer unable read the error message and therefore unable to fix the error
  • Do not use die() or echo or any other function to print the error message on the screen unconditionally. PHP can report errors by itself and do it the right way depends on the environment - so just leave it for PHP.
  • Do not add a condition to test the query result manually (like if($result)). With error exceptions enabled such condition will just be useless.
  • Do not use try..catch operator for echoing the error message. This operator should be used to perform some error handling, like a transaction rollback. But never use it just to report errors - as we learned above, PHP can already do it, the right way.

附言
有时没有错误,但也没有结果.那么这意味着,数据库中没有符合您条件的数据.在这种情况下,你必须承认这个事实,即使你可以发誓数据和标准都没有问题.他们不是.你必须再次检查它们.我有一篇文章可以帮助解决这个问题,如何调试数据库交互.虽然是为PDO写的,但是原理是一样的.只需一步一步地按照此说明进行操作,要么解决您的问题,要么为 Stack Overflow 提供一个可回答的问题.

P.S.
Sometimes there is no error but no results either. Then it means, there is no data in the database to match your criteria. In this case you have to admit this fact, even if you can swear the data and the criteria are all right. They are not. You have to check them again. I've got an article that can help in this matter, How to debug database interactions. Although it is written for PDO, but the principle is the same. Just follow this instruction step by step and either have your problem solved or have an answerable question for Stack Overflow.

这篇关于mysqli_fetch_assoc() 期望参数/调用成员函数 bind_param() 错误.如何获取实际的 mysql 错误并修复它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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