-1

I am getting this error when I execute my page:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource ...

I have tried running the SQL directory on phpMyAdmin and it runs fine.

Here is the full code:

<?php

$connect_error = 'Sorry, we have connection problems.';

mysql_connect('localhost','user','password') or die($connect_error);
mysql_select_db('mydb') or die($connect_error);



$result = mysql_query("SELECT * FROM tbl_main ORDER BY id desc limit 1");
 $rows = array();


   while($r = mysql_fetch_assoc($result)) {  //ERROR POINTS HERE
     $rows['id'][] = $r;
   } 

 print json_encode($rows);



?>

Why I'm I getting this error?

Satch3000
  • 47,356
  • 86
  • 216
  • 346
  • what does mysql_error() produce after mysql_query() is executed? – Dennis Haarbrink Oct 22 '12 at 09:50
  • Try to ouput the MySQL error using `mysql_error()`. – Florent Oct 22 '12 at 09:50
  • 3
    Do proper error checking as pointed out in the examples in the manual. http://php.net/manual/en/function.mysql-query.php that will give you a meaningful error message from mySQL. – Pekka Oct 22 '12 at 09:51
  • 1
    You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) – Quentin Oct 22 '12 at 09:53
  • Duplicate: http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select – hakre Oct 22 '12 at 10:12

1 Answers1

1

If mysql_query returns FALSE, then you'll get an error. It can return false if there is a problem with you SQL, or there is a problem with your database connection.

Call mysql_error() to find out more about the error that occured.

Also, you should really be using PDO or MySQLi with PHP now.

<?php

$connect_error = 'Sorry, we have connection problems.';

$link = mysql_connect('localhost','user','password') or die($connect_error);
mysql_select_db('mydb', $link) or die($connect_error);

$result = mysql_query("SELECT * FROM tbl_main ORDER BY id desc limit 1", $link);
if ($result) {
    $rows = array();

    while($r = mysql_fetch_assoc($result)) {  //ERROR POINTS HERE
        $rows['id'][] = $r;
    } 

    print json_encode($rows);
} else {
    print mysql_error($link);
}

?>
Rik Heywood
  • 13,816
  • 9
  • 61
  • 81