3

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++;
}
Community
  • 1
  • 1
  • `var_dump($results, mysql_error());` – zerkms Sep 13 '12 at 20:53
  • 1
    off topic, but important: please note that PHP strongly recommends not using the `mysql_xx()` functions. These functions are considered obsolete and are no longer maintained. They recommend switching to either the newer `mysqli_xx()` functions or the PDO library. – Spudley Sep 13 '12 at 21:29

4 Answers4

2

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

mishmash
  • 4,422
  • 3
  • 34
  • 56
  • 1
    Better yet, use `OR die(mysql_error())` instead. – bfavaretto Sep 13 '12 at 20:55
  • when die is executed you wouldn't see the rest of the code, so... no, that's not it – SparK Sep 13 '12 at 20:55
  • @SparK yeah I thought of that now. Stupid of me. bfavaretto: its the same thing. :P – mishmash Sep 13 '12 at 20:56
  • @vanneto `OR` and `||` have different precedence, you must use `OR` in this case. – bfavaretto Sep 13 '12 at 20:57
  • @bfavaretto true, I guess you learn something new every day. Thank you. – mishmash Sep 13 '12 at 20:58
  • @vanneto....bitwise vs. logical – Emory Barlow Sep 13 '12 at 20:59
  • 2
    @EmoryBarlow No, both are boolean/logic operators. But PHP has those oddities, like two different logic operators for the same thing, with different precedence... – bfavaretto Sep 13 '12 at 21:00
  • @vanneto If you edit your answer to suggest the `OR`, you get a free upvote from me :) – bfavaretto Sep 13 '12 at 21:01
  • Even better yet, don't use `die()` at all. Errors should be properly handled and a meaningful message displayed to the user. mysql_error() might give away your internal database structure too. – AndrewR Sep 13 '12 at 21:02
  • @AndrewR -- I was using die() just for development purposes...@bfavaretto -- Thanks for the info, new to PHP. Normally a hardware designer (mostly in C) so I'm still learning. – Emory Barlow Sep 13 '12 at 21:06
  • @vanneto RE: 'Its just another one of PHP's inconsistencies' Maybe it's inconsistent, but PHP is (one of) the only languages that give you the option of having logical comparison operators that evaluate before or after the assignment operators. I'd say that makes it more powerful, if you know how to use it. Of the other languages that I use, C#, Javascript and Java only have && and || which both evaluate before the assignment operators, and Python only has AND and OR, which evaluate at a lower precedence than the assignment operators. – AndrewR Sep 14 '12 at 19:52
1

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

John
  • 11
  • 2
0
if ($results) { while...

Make sure you actually have results

wesside
  • 5,622
  • 5
  • 30
  • 35
0

I believe there is something wrong with your SQL Query, check that the table and column names are correct, check that you have a connection to the DB and also as bfavaretto says use or die(mysql_error()) instead.

Im not 100% sure but shouldnt the query be:

$query = "select distinct s.time, s.parameter, s.data, t.units as s, parameters as t from alertData 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";

instead of

$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";
Martin
  • 11
  • 1