Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given [duplicate]

Tags:

php

mysql

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

The line it references is the line where the mysql_fetch_array() function is called. The query works fine when ran through phpmyadmin. It also throws no errors. Any help would be appreciated.

$query = "select distinct s.time, s.parameter, s.data, t.units from alertData as s, parameters as t where s.parameter like '%Airtemp_Avg%' and s.staID = 'WS_001_UHC' and s.interval_min = 15 and t.parameter like '%Airtemp_Avg%' and unix_timestamp(s.time) >= (unix_timestamp(now()) - 86400) order by time desc";
$results = mysql_query($query) || die(mysql_error());
$dataCnt = 0;
while($info = mysql_fetch_array($results)) {
    //15 Min data
    if(($dataCnt == 0) && (getTimestamp($info['time']) >= ($now - 4500))) 
       $data15['temp'] = $info['data'];
    else 
       $data15['temp'] = '-';

    $dataCnt++;
}
like image 816
Emory Barlow Avatar asked Mar 23 '26 21:03

Emory Barlow


2 Answers

Remove the || die (mysql_error()) after the mysql_query(). Its being evaluated as a bool and that is causing the error.

EDIT:

As bfavaretto noted, you could use OR instead. Its just another one of PHP's inconsistencies. Read more about it in the PHP Documentation about Logical Operators (take a look in the comments of the first code sample).

like image 164
mishmash Avatar answered Mar 26 '26 11:03

mishmash


$results will return false if there is an error.

Use something like this to check your results:

if (false === $result) {
echo mysql_error();
}

Also, mysql_ functions are not recommended and are being deprecated. Use PDO or MySQLi instead.

like image 23
John Avatar answered Mar 26 '26 09:03

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!