1

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 Warning: 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

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 5.3.2 (cli) (built: Jun 25 2011 08:12:19)

Copyright (c) 1997-2010 The PHP Group

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

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

@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;
    }
}
RAS
  • 8,100
  • 16
  • 64
  • 86
danielj23
  • 83
  • 1
  • 1
  • 5
  • 1
    @bažmegakapa: Thanks for taking care making this at least a bit more canonical. Much appreciated. – hakre Jul 18 '12 at 08:18

2 Answers2

3

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

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().

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.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • Indeed. The connection was failing due to an inaccurate database call. I was calling a non-existent database. Your answer, pointed me in the right direction. Thank you – danielj23 Oct 09 '11 at 22:40
0

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

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

Max Allan
  • 640
  • 5
  • 18
  • I'm fairly certain it's just a wrapper around mysql_query(). His queryFetch seems to imply that it calls mysql_query then returns mysql_fetch_assoc on that. – Corbin Oct 09 '11 at 22:29
  • The problem was the database was not connecting. HOWEVER, your response is somewhat accurate. query works synonymous with mysql_query My problem ultimately ended up being in the connection string define('MYSQL_DATABASE', 'database'); when in fact it should have been define('MYSQL_DATABASE', 'repair_database'); – danielj23 Oct 09 '11 at 22:38