php 如何防止此错误:警告:mysql_fetch_assoc() 期望参数 1 为资源,布尔值在第 11 行中给出...
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3129374/
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
how to prevent this error : Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in ... on line 11
提问by Naughty.Coder
Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
I'm very confused with this error, it shows when I try to return a result from the DB that doesn't exist ... I tried mysql_num_rows()but it returns the same error but instead of mysql_fetch_assocexpects ... it says mysql_num_rows()expects ...
我对这个错误很困惑,当我尝试从不存在的数据库返回结果时它会显示......我尝试过mysql_num_rows()但它返回相同的错误但不是mysql_fetch_assoc期望......它说mysql_num_rows()期望......
I set error_reporting(0)to avoid showing this error, but I'm not satisfied with this solution ...
我设置error_reporting(0)避免显示此错误,但我对这个解决方案不满意......
回答by timdev
Here's the proper way to do things:
这是做事的正确方法:
<?PHP
$sql = 'some query...';
$result = mysql_query($q);
if (! $result){
throw new My_Db_Exception('Database error: ' . mysql_error());
}
while($row = mysql_fetch_assoc($result)){
//handle rows.
}
Note the check on (! $result) -- if your $result is a boolean, it's certainly false, and it means there was a database error, meaning your query was probably bad.
注意 (!$result) 上的检查——如果你的 $result 是一个布尔值,它肯定是错误的,这意味着有一个数据库错误,意味着你的查询可能是错误的。
回答by Pavel Strakhov
You must check if result returned by mysql_query is false.
您必须检查 mysql_query 返回的结果是否为假。
$r = mysql_qyery("...");
if ($r) {
mysql_fetch_assoc($r);
}
You can use @mysql_fetch_assoc($r)to avoid error displaying.
您可以使用@mysql_fetch_assoc($r)来避免错误显示。
回答by cypher
The proper syntax is (in example):
正确的语法是(在示例中):
$query = mysql_query('SELECT * FROM beer ORDER BY quality');
while($row = mysql_fetch_assoc($query)) $results[] = $row;
回答by Joseph Mansfield
This is how you should be using mysql_fetch_assoc():
这是你应该如何使用 mysql_fetch_assoc():
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
// Do stuff with $row
}
$result shouldbe a resource. Even if the query returns no rows, $result is still a resource. The only time $result is a boolean value, is if there was an error when querying the database. In which case, you should find out what that error is by using mysql_error()and ensure that it can'thappen. Then you don't have to hide from any errors.
$result应该是一个资源。即使查询没有返回任何行,$result 仍然是一个资源。唯一一次 $result 是布尔值,是在查询数据库时出现错误。在这种情况下,您应该使用mysql_error()找出该错误是什么,并确保它不会发生。这样您就不必躲避任何错误。
You should always cover the base that errors mayhappen by doing:
您应该始终通过执行以下操作来涵盖可能发生错误的基础:
if (!$result) {
die(mysql_error());
}
At least then you'll be more likely to actually fix the error, rather than leave the users with a glaring ugly error in their face.
至少这样你更有可能真正修复错误,而不是让用户面对明显的丑陋错误。
回答by Your Common Sense
You don't need to prevent this error message!
Error messages are your friends!
Without error message you'd never know what is happened.
It's all right! Any working code supposed to throw out error messages.
您不需要阻止此错误消息!
错误消息是您的朋友!
如果没有错误消息,您永远不会知道发生了什么。
没关系!任何工作代码都应该抛出错误消息。
Though error messages needs proper handling. Usually you don't have to to take any special actions to avoid such an error messages. Just leave your code intact. But if you don't want this error message to be shown to the user, just turn it off. Not error message itself but daislaying it to the user.
尽管错误消息需要正确处理。通常您不必采取任何特殊措施来避免此类错误消息。保持你的代码完好无损。但是,如果您不希望向用户显示此错误消息,只需将其关闭即可。不是错误消息本身,而是将其显示给用户。
ini_set('display_errors',0);
ini_set('log_errors',1);
or even better at .htaccess/php.ini level
And user will never see any error messages. While you will be able still see it in the error log.
Please note that error_reporting should be at max in both cases.
甚至在 .htaccess/php.ini 级别更好,
用户永远不会看到任何错误消息。虽然您仍然可以在错误日志中看到它。
请注意,在这两种情况下,error_reporting 都应为最大值。
To prevent this message you can check mysql_query result and run fetch_assoc only on success.
But usually nobody uses it as it may require too many nested if's.
But there can be solution too - exceptions!
为防止出现此消息,您可以检查 mysql_query 结果并仅在成功时运行 fetch_assoc。
但通常没有人使用它,因为它可能需要太多嵌套的 if。
但也可以有解决方案 -例外!
But it is still not necessary. You can leave your code as is, because it is supposed to work without errors when done.
但这仍然没有必要。你可以保留你的代码,因为它应该在完成后没有错误地工作。
Using returnis another method to avoid nested error messages. Here is a snippet from my database handling function:
使用return是另一种避免嵌套错误消息的方法。这是我的数据库处理函数的一个片段:
$res = mysql_query($query);
if (!$res) {
trigger_error("dbget: ".mysql_error()." in ".$query);
return false;
}
if (!mysql_num_rows($res)) return NULL;
//fetching goes here
//if there was no errors only
回答by Computerish
If you just want to suppress warnings from a function, you can add an @sign in front:
如果你只是想抑制来自函数的警告,你可以@在前面添加一个符号:
<?php @function_that_i_dont_want_to_see_errors_from(parameters); ?>

