1

I created database on localhost, wrote some pup code. When I run localhost I get the error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in:

This is php code:

<?php
    require('connect_review.php');
    $query=mysql_query("SELECT * FROM reviews ORDER BY id DESC");


 while($rows=mysql_fetch_assoc($query))
     {

         $id=$rows['idname'];
         $dname=$rows['iname'];
         $dcomment=$rows['icomment'];
         echo '<font color="#02AAFC">Added:</font>  ' . $dname . '<br />' .  $dcomment . '&nbsp' . '&nbsp' .
          '&nbsp' . '&nbsp' . '<br />' . '<br />' ;

    }
    ?>

This is result:

enter image description here

Jason
  • 15,017
  • 23
  • 85
  • 116
HomerDoRock
  • 23
  • 2
  • 5
  • Apparently, mysql_query returns false. mysql_query returns false on error. Try to echo mysql_error() after that line for some more information. – goto-bus-stop Jan 08 '14 at 22:33
  • 2
    Don't use `mysql_*()` for new code: it's deprecated. Use `mysqli_*()` or `PDO` –  Jan 08 '14 at 22:35
  • 2
    Please read this - [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Jan 08 '14 at 22:36
  • You should append `or die(mysql_error())` to the end of your query to see what error it throws. – sveti petar Jan 08 '14 at 22:55
  • @jovan Whilst the sentiment is good, one should *never* use `or die` – Phil Jan 08 '14 at 23:01
  • I'm not saying leave it in for production, but it would effectively tell him what's wrong with his query. – sveti petar Jan 09 '14 at 09:46

1 Answers1

-1

As others have mentioned, you likely want to migrate away from mysql_* functions. However, if you insist on continuing this way, you should check that rows were returned before issuing your mysql_fetch_assoc

Sample code:

....
$num_rows = 0
if ($query) {
  $num_rows = mysql_num_rows($query)
  if ($num_rows) {
      while($rows=mysql_fetch_assoc($query))
      ...
  }
}
ThatOneDude
  • 1,516
  • 17
  • 20
  • Missing a `$` there, plus missing the fact that it returns `false` if it never worked out in the first place. – tadman Jan 08 '14 at 22:57
  • Edited to fix missing $'s. mysql_num_rows will correctly return false if $query is false, no need to additionally (and unnecessarily) test for query being false. – ThatOneDude Jan 08 '14 at 23:29
  • This will simply return the same error if the query is faulty; *"mysql_num_rows expects parameter 1 to be resource, boolean given"*. This is **not** a solution – Phil Jan 09 '14 at 01:00
  • Thanks @Phil and tadman. Error reporting was turned up too high and I was not seeing warning but was seeing correct behavior. Edited code above to explicitly check query returned non-FALSE before proceeding. – ThatOneDude Jan 09 '14 at 01:29
  • Your *num rows* check is redundant here. You might as well leave it out all together and head straight into the `while` loop after checking the validity of the query. Of course, that would then make this answer exactly the same as all the other answers to this question (see possible duplicates above) – Phil Jan 09 '14 at 01:32
  • The check makes it so that if I you get 0 rows returned from the query you won't unnecessarily mysql_fetch_assoc($query) which will just return false. But yes, I don't disagree that this could be just as easily solved by the code in the linked-to duplicate suggestion. The secondary benefit of mysql_num_rows is that you could turn the while loop into a for loop with condition < num_rows but that is neither here nor there and according to phpbench.com may actually be slower. – ThatOneDude Jan 09 '14 at 01:44