$num_rows not working: what have I done???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

$num_rows not working: what have I done???

Post by simonmlewis »

Code: Select all

$result = mysql_query ("SELECT catid, catname FROM products WHERE pause = 'off' AND catid IS NOT NULL GROUP BY catname");
while ($row = mysql_fetch_object($result))
      {
      $count = mysql_query ("SELECT id, catid FROM products WHERE catid = '$row->catid");
      $num_rows = mysql_num_rows($count); 
      mysql_free_result($count);
      
			echo "<div class='submenu'";
			
      if ($c != NULL && $c == $row->catid) { echo "style='background-color:#FF7200; color: #ffffff'";}
			echo "><a href='index.php?page=categ&menu=sub&c=$row->catid&cname=$row->catname&head=$row->catname'>";
			
      if ($c == $row->catid) { echo "<font color='#000000'>$row->catname&nbsp;<font size='1'>($num_rows)</font></a></font>";} 
      elseif ($c != $row->catid) { echo "$row->catname&nbsp;<font size='1'>($num_rows)</font></a>";}
      echo "</div>";
        
        }
	mysql_free_result($result);
When I run it live I just get the () brackets around what should be a count.
When I run it locally I get this:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\menu\sub.inc on line 13

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\menu\sub.inc on line 15
Line 13 is

Code: Select all

      $num_rows = mysql_num_rows($count); 
Line 15 is empty.

I just need to have the category name, and the amount of items assigned to that category by each one.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: $num_rows not working: what have I done???

Post by Eran »

This is what happens when you don't check the results of the query. It is returning 'false'.
You have a missing single-quote at the end of the query, or alternatively, if catid is a number, you have a single-quote that is not needed in front of it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: $num_rows not working: what have I done???

Post by simonmlewis »

Oh what a spanner.
Thanks!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply