php mysqli_fetch_assoc() 期望参数/调用成员函数 bind_param() 错误。如何获取实际的mysql错误并修复它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22662488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysqli_fetch_assoc() expects parameter / Call to a member function bind_param() errors. How to get the actual mysql error and fix it?
提问by siopaoman
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:
在我的本地/开发环境中,MySQLi 查询执行正常。但是,当我将其上传到我的网络主机环境时,出现此错误:
Fatal error: Call to a member function bind_param() on a non-object in...
致命错误:在...中的非对象上调用成员函数 bind_param()
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.
为了检查我的查询,我尝试通过控制面板 phpMyAdmin 执行查询,结果正常。
回答by Your Common Sense
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.
有时候,你的库MySQLi代码产生这样的错误mysqli_fetch_assoc() expects parameter...
,Call to a member function bind_param()...
或类似的。或者甚至没有任何错误,但查询并不完全相同。这意味着您的查询未能执行。
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.
每次查询失败时,MySQL 都会有一条错误消息来解释原因。不幸的是,默认情况下,此类错误不会传输到 PHP,您所得到的只是上面提到的神秘错误消息。因此,配置 PHP 和 MySQLi 以向您报告 MySQL 错误非常重要。一旦您收到错误消息,修复它将是小菜一碟。
How to get the error message in MySQLi?
如何在 MySQLi 中获取错误消息?
First of all, always have this line before MySQLi connect in allyour environments:
首先,在所有环境中连接 MySQLi 之前,请始终使用此行:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
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.
之后,所有 MySQL 错误都将转换为 PHP 异常。反过来,未捕获的异常会导致 PHP 致命错误。因此,如果出现 MySQL 错误,您将收到常规的 PHP 错误。这将立即让您知道错误原因。堆栈跟踪将引导您到发生错误的确切位置。
How to configure PHP in different environments
不同环境下如何配置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.
这是我关于PHP 错误报告的文章的要点:
在开发和实时服务器上报告错误必须不同。在开发服务器上,在屏幕上显示错误很方便,但在实时服务器上,必须记录错误消息,以便您稍后可以在错误日志中找到它们。
Therefore, you must set corresponding configuration options to the following values:
因此,您必须将相应的配置选项设置为以下值:
On a development server
error_reporting
should be set toE_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
On a production server
error_reporting
should be set toE_ALL
value;log_errors
should be set to 1display_errors
should be set to 0
在开发服务器上
error_reporting
应设置为E_ALL
值;log_errors
应该设置为1(在开发PC上也有日志很方便)display_errors
应该设置为 1
在生产服务器上
error_reporting
应设置为E_ALL
值;log_errors
应该设置为 1display_errors
应该设置为0
How to actually use it?
如何实际使用它?
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:
只需删除任何手动检查错误的代码,所有这些or die()
,if ($result)
等等。只需立即编写数据库交互代码:
$stmt = $this->con->prepare("INSERT INTO table(name, quantity) VALUES (?,?)");
$stmt->bind_param("si", $name, $quantity);
$stmt->execute();
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).
再次,周围没有任何条件。如果发生错误,它将被视为代码中的任何其他错误。例如,在开发 PC 上,它只会出现在屏幕上,而在实时站点上,它将为程序员记录,而为了用户的方便,您可以使用错误处理程序(但这是一个不同的故事,它与主题无关) MySQLi,但您可以在上面链接的文章中阅读它)。
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 numberof 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 traceto 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, 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 beforethe query part cited in the error message.
- 比如说,如果它说一个特定的表不存在,你必须检查拼写、错别字、字母大小写。此外,您必须确保您的 PHP 脚本连接到正确的数据库
- 或者,如果它说 SQL 语法有错误,那么您必须检查您的 SQL。问题点就在错误消息中引用的查询部分之前。
If you don't understand the error message, try to Google it. And when browsing the results, stick to answers that explainthe 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 trustthe error message. If it says that number of tokens doesn't match the number of bound variables then it isso. 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.
您还必须信任错误消息。如果它说令牌的数量与绑定变量的数量不匹配,那么就是这样。缺失的表或列也是如此。给定选择,无论是你自己的错误还是错误信息是错误的,始终坚持前者。再次听起来居高临下,但这个网站上的数百个问题证明这个建议非常有用。
A list of things you should never ever do in regard of error reporting
关于错误报告你永远不应该做的事情清单
- 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()
orecho
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.
- 切勿使用错误抑制运算符 (
@
)!它使程序员无法读取错误消息,因此无法修复错误 - 不要使用
die()
或echo
或任何其他功能无条件地在屏幕上打印错误消息。PHP 可以自行报告错误,并根据环境以正确的方式执行此操作 - 所以将其留给 PHP。 - 不要添加条件来手动测试查询结果(如
if($result)
)。启用错误异常后,这种情况将毫无用处。 - 不要使用
try..catch
运算符来回显错误消息。这个操作符应该用于执行一些错误处理,比如事务回滚。但是永远不要仅仅使用它来报告错误——正如我们在上面了解到的,PHP 已经可以做到这一点,而且是正确的方式。
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.
PS
有时没有错误但也没有结果。那么这意味着,数据库中没有与您的标准相匹配的数据。在这种情况下,你必须承认这个事实,即使你可以发誓数据和标准都没有问题。他们不是。你必须再次检查它们。我有一篇文章可以帮助解决这个问题,如何调试数据库交互。虽然是为PDO写的,但是原理是一样的。只需一步一步地按照此说明进行操作,要么解决您的问题,要么为 Stack Overflow 提供一个可回答的问题。