rallokkcaz Posted September 4, 2009 Share Posted September 4, 2009 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 More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 Why are you looping through a result executing more queries in the first place? Very inefficient. Theres a tutorial on UNIONS and JOINS on our main site, you should read it. Link to comment https://forums.phpfreaks.com/topic/173064-mysql_num_rows-strange-behavior/#findComment-912194 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 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(); } Link to comment https://forums.phpfreaks.com/topic/173064-mysql_num_rows-strange-behavior/#findComment-912251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.