Jump to content

mysql_num_rows() strange behavior


rallokkcaz

Recommended Posts

Okay guys, mysql_num_rows() is acting really strange, when ever i call it in my loop, it doesn't allow me to display other records that i need to come up.

For instance I'm checking to see if a category has a subcategory but it isn't showing the categories that don't have subcategories.

Here's the code:

$sub_cats = mysql_query("SELECT * FROM `categories` WHERE sub = '". $result['name'] ."'");
while($row = mysql_fetch_array($sub_cats))
{
$query = mysql_query("SELECT * FROM `categories` WHERE sub = '". $row['name'] ."'");
$rows_count = mysql_num_rows($query) or die(mysql_error());
if($rows_count > 0)
{
	echo "<div class=\"category\" onclick=\"getCategories('". $row['name'] ."'); return false;\">". $rows_count ." ".  UCWords($row['name']) ."</div>";
}
else 
{
                //Just to check if it would even show the rows without sub-categories.
	echo "DSAKFKASDSA";
}
}

Anyone have any idea's why this is happening, or is this just some dumb mistake i'm making because it's been awhile since i've used PHP.

 

-Zack

Link to comment
https://forums.phpfreaks.com/topic/173064-mysql_num_rows-strange-behavior/
Share on other sites

Try this, and let me know what it does...

 

if ($sub_cats = mysql_query("SELECT * FROM `categories` WHERE sub = '". $result['name'] ."'")) {

if(mysql_num_rows($sub_cats) > 0) {
	while($row = mysql_fetch_array($sub_cats)) {

		$query = mysql_query("SELECT * FROM `categories` WHERE sub = '". $row['name'] ."'");
		$rows_count = mysql_num_rows($query);

		if($rows_count > 0) {
			echo "<div class=\"category\" onclick=\"getCategories('" . $row['name'] ."'); return false;\">". $rows_count ." ".  UCWords($row['name']) ."</div>";
		} else {
			echo '$query query returned no results.';
		}
	}
} else {
echo '$sub_cats query returned no results.';
}
} else {
echo '$sub_cats query failed: ' . mysql_error();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.