-1

I got the error "mysql_fetch_array(): supplied argument is not a valid MySQL result resource...". On the lines that concern this mysql query.

$result = mysql_query("select * from chat order by time desc limit 0,10");
$messages = array();

while($row = mysql_fetch_array($result)){

   $messages[] = "<div class='message'><div class='messagehead'>" . $row[name] . " - " . date('g:i A M, d Y',$row[time]) . "</div><div class='messagecontent'>" . $row[message] . "</div></div>";
   //The last posts date
   $old = $row[time];
}
//Display the messages in an ascending order, so the newest message will be at the bottom
for($i=9;$i>=0;$i--){
   echo $messages[$i];
}
halfer
  • 19,824
  • 17
  • 99
  • 186
user2245512
  • 107
  • 1
  • 2
  • 5
  • I imagine your query is failing; but you're not checking for that, and just assume it's worked. You should be checking the return from `mysql_query` and handling it appropriately; you'll find the full details of the problem in `mysql_error`. – andrewsi Aug 29 '13 at 12:52
  • possible duplicate of [supplied argument is not a valid MySQL result resource](http://stackoverflow.com/questions/7214280/supplied-argument-is-not-a-valid-mysql-result-resource) – mario Aug 29 '13 at 12:52
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ) – Fracsi Aug 29 '13 at 12:53
  • I think no results found first need to check mysql_num_rows then check fetch array. – harsh4u Aug 29 '13 at 12:53
  • 1
    Have you done any basic debugging here? Make use of `mysql_error` function to see what's wrong with your query. It also helps if you search through the millions of duplicates in the site that has already been answered. – Amal Murali Aug 29 '13 at 12:54
  • 1
    Yor next notice will be: Use of undefined constant name - assumed 'name' and ... – Vahid Hallaji Aug 29 '13 at 12:58
  • Your connection could have failed too, so check that. – halfer Aug 29 '13 at 13:09

2 Answers2

1

Theres an error in your query.

Try to output the error with mysql_error(), e.g.:

$result = mysql_query("select * from chat order by time desc limit 0,10") or die(mysql_error());

This will stop the script and show the sql error.

virtualmarc
  • 533
  • 1
  • 4
  • 16
0

It means your query is not executed there is some error in it.

you can find out what the error is using mysql_error()

or

you can use

$row = mysql_num_rows($result);
echo($row);

see if it returns any value. If it returns 1 or value >1 it means your query is working fine else query is not executed.

Pooja
  • 170
  • 1
  • 1
  • 10