php mysql_fetch_assoc() 期望参数 1 是资源,布尔值给定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7706973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:12:54  来源:igfitidea点击:

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

phpsql

提问by danielj23

I recently upgraded my PHP version on my host and I think it is causing this error. Whenever I start apache and this include file is called to access the database, apache starts generating GIGABYTES of errors. I view the log and I receive this error

我最近在我的主机上升级了我的 PHP 版本,我认为它导致了这个错误。每当我启动 apache 并调用此包含文件来访问数据库时,apache 就会开始生成 GIGABYTES 的错误。我查看日志并收到此错误

PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/www/html/includes/database.config.php on line 22

PHP警告: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/www/html/includes/database.config.php on line 22

Line 22 is

第 22 行是

while(($c = mysql_fetch_assoc($rsetCoupons)) !== false)

I am guessing that it will generate that error on every place this is listed.

我猜它会在列出的每个地方产生该错误。

Does anyone have any idea on what could be causing this? The current PHP version is

有没有人知道可能导致这种情况的原因?当前的 PHP 版本是

PHP 5.3.2 (cli) (built: Jun 25 2011 08:12:19)

PHP 5.3.2 (cli)(构建时间:2011 年 6 月 25 日 08:12:19)

Copyright (c) 1997-2010 The PHP Group

版权所有 (c) 1997-2010 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

Zend Engine v2.3.0,版权所有 (c) 1998-2010 Zend Technologies

(obviously I cut out the variables for the connect at the beginning of the code)

(显然我在代码的开头剪掉了connect的变量)

@mysql_connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD);
@mysql_select_db(MYSQL_DATABASE);

define('DOMAIN', 'MYDOMAINISTHIS.com');

$parse_version = queryFetch('SELECT version,secret FROM version ORDER BY version_id DESC LIMIT 0,1');

$VERSION = $parse_version['version'];
$SECRET = $parse_version['secret'];

$VALID_COUPONS = array();

$rsetCoupons = query('SELECT * FROM coupons ORDER BY coupon_id ASC');

while(($c = mysql_fetch_assoc($rsetCoupons)) !== false)
{
    $VALID_COUPONS[$c['code']] = $c['percent'];
}

$salutations = array();

$rsetSalutations = query('SELECT * FROM salutations ORDER BY salutation_id ASC');

while(($c = mysql_fetch_assoc($rsetSalutations)) !== false)
{
    $salutations[] = $c['salutation'];
}

$BASE_PRICE_QTY = array();
$UPGRADE_PRICE = array();

$rsetPrices = query('SELECT * FROM cart_prices ORDER BY qty ASC');

while(($c = mysql_fetch_assoc($rsetPrices)) !== false)
{
    $BASE_PRICE_QTY[] = $c['unit_price'];
    $UPGRADE_PRICE[] = $c['upgrade_price'];
}

function insert($hash, $table)
{
    $fields = implode(',', array_keys($hash));
    $values = implode('","', $hash);

    $query = sprintf('INSERT INTO %s (%s) VALUES("%s")', $table, $fields, $values);

    query($query);
}

function query($query)
{
    return @mysql_query($query);
}

function queryFetch($query)
{
    return @mysql_fetch_assoc(query($query));
}

function p($key, $default = '')
{
    if (isset($_POST[$key]))
    {
        return $_POST[$key];
    }
    else 
    {
        return $default;
    }
}

function g($key, $default = '')
{
    if (isset($_GET[$key]))
    {
        return $_GET[$key];
    }
    else 
    {
        return $default;
    }
}

回答by Corbin

$rsetCoupons is not a mysql query resource at that point in execution. Either your query is failing, or the variable is being lost somewhere.

$rsetCoupons 在执行时不是 mysql 查询资源。您的查询失败,或者变量在某处丢失。

http://php.net/mysql_fetch_assoc

http://php.net/mysql_fetch_assoc

I'm fairly certain you're query is failing. You should check the return of mysql_query and if it's false, then check mysql_error().

我很确定你的查询失败了。您应该检查 mysql_query 的返回值,如果它为 false,则检查 mysql_error()。

Also, you should not suppress errors in your mysql_connect and mysql_select_db calls. If the database connection cannot be made, you should handle that more gracefully than letting your page trod on and error on every subsequent mysql call. That may actually be what your error is. If you're suppressing errors to hide them from users, public facing PHP sites should have display_errors set to off, but you should still be logging errors.

此外,您不应抑制 mysql_connect 和 mysql_select_db 调用中的错误。如果无法建立数据库连接,您应该更优雅地处理它,而不是让您的页面在每次后续 mysql 调用时都被踩到并出错。这实际上可能是您的错误所在。如果您要抑制错误以对用户隐藏它们,面向公众的 PHP 站点应该将 display_errors 设置为关闭,但您仍然应该记录错误。

回答by Max Allan

This answer is meaning (often) that your QUERY-syntax is wrong.

这个答案意味着(通常)您的 QUERY 语法是错误的。

In this case you are using $rsetCoupons = query('SELECT * FROM coupons ORDER BY coupon_id ASC');and then a mysql function, use mysql_queryinstead!

在这种情况下,您使用的$rsetCoupons = query('SELECT * FROM coupons ORDER BY coupon_id ASC');是 mysql 函数,请mysql_query改用!