0

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

I am working on a project for my senior class, and our lead programmer just flunked and now I am the sole programmer. He was using PHP and I have pretty much no programming experience. The program keeps kicking back this error...

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in (points to the pastebin file on line 33)

http://pastebin.com/nfv9P2h5

If you guys could help I would be very appreciative... I can't seem to figure this out.

Thanks,

Nathan

UPDATE: I ran the echo mysql_error flag and it came up with a error to a row 58 which is

if($rows == ''){
Community
  • 1
  • 1
  • You have some kind of issue with the sql query. You need to check $result. If it's false you need to print mysql_error() to see what went wrong. – Cfreak Mar 07 '12 at 04:51

2 Answers2

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

This error almost always points to a failed SQL query, which is usually a syntax issue. I'm looking into your code now, but I suggest you double check your query syntax.

mysql_query returns a resource upon completing a successful query, or a false if the query fails. Your query is failing, and consequently, you are passing the false to mysql_fetch_assoc() which expects a resource, hence the error.

You could (should?) do it this way

$result = mysql_query($query);
if (!$result)
   echo mysql_error();
else
   //do your stuff here

If you cannot eye-ball the error in your query, use the above code snippet. mysql_error() will report the error, so you know where you're going wrong. Alternatively, run the query in an IDE such as MySQL Workbench to debug the query.

xbonez
  • 40,730
  • 48
  • 157
  • 236
  • I really appreciate it! I'm looking now as well. – user1253828 Mar 07 '12 at 04:52
  • Thanks, I'm going to try what Jon posted, and if there is nothing that works still I'll use this method to see if it can help narrow it down. – user1253828 Mar 07 '12 at 04:59
  • I've made some progress... after placing the error codes in here is what they tell me. Notice: Undefined variable: rows in C:\xampp\htdocs\report.php on line 60 Notice: Undefined variable: rows in C:\xampp\htdocs\report.php on line 65 – user1253828 Mar 07 '12 at 05:13
  • Ok, this means your SQL part is working fine. The error now reported is a PHP error. In your pastebin, in line 46 you create and use $rows. Then again, you reference it in line 56, but it is no longer in scope. Add `$rows = array();` between line 32 and 33. – xbonez Mar 07 '12 at 05:46
0

This means that your query returned no results (The problem is in the SQL, not the PHP). It could be a syntax error, but I didn't immediately see one.

Try replacing $query with this:

SELECT *
FROM counterdata

This is guaranteed to return something (as long as you have some data in your database), and should work if nothing else is wrong.

Then, try slowly adding parts of the SQL to the query again, like this:

// Next Step:
SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate
FROM counterdata

// Then Try:
SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate
FROM counterdata
WHERE StartDate >= '{$yesterdayTimestamp}'

// And Lastly:
SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate
FROM counterdata
WHERE StartDate >= '{$yesterdayTimestamp}'
GROUP BY StartDate, SensorName

This will tell you where the errors are - if any exist - in your SQL. Otherwise, it should work.

If none of this works, then something in the PHP is wrong, but I didn't see anything wrong when I looked at it.

Jon Egeland
  • 12,064
  • 8
  • 46
  • 62