如何防止该错误:警告:mysql_fetch_assoc()期望参数1为资源,在第11行的...中给定布尔值 [英] how to prevent this error : Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in ... on line 11

查看:107
本文介绍了如何防止该错误:警告:mysql_fetch_assoc()期望参数1为资源,在第11行的...中给定布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
PHP错误:mysql_fetch_array()期望参数1是资源,给定布尔值

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

我对这个错误非常困惑,它显示了当我尝试从不存在的数据库中返回结果时...我尝试了mysql_num_rows(),但是它返回了相同的错误,但不是mysql_fetch_assoc ...说mysql_num_rows()期望...

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_assoc expects ... it says mysql_num_rows() expects ...

我设置了error_reporting(0)以避免显示此错误,但是我对这种解决方案不满意...

I set error_reporting(0) to avoid showing this error, but I'm not satisfied with this solution ...

推荐答案

以下是正确的处理方式:

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.
}

请注意(!$ result)的检查-如果$ result是布尔值,则肯定为假,这意味着存在数据库错误,这意味着您的查询可能不正确.

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.

这篇关于如何防止该错误:警告:mysql_fetch_assoc()期望参数1为资源,在第11行的...中给定布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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