ballhogjoni Posted July 28, 2007 Share Posted July 28, 2007 I am creating an infinite loop with this code. My goal is to just ECHO the amount of rows in my table according to a specific Username. How do I do this? my code <?php $query = "SELECT ID FROM products WHERE Username = '{$Username}'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_num_rows($result) or die(mysql_error()); $num = 0; while ($num <= $row) { echo $row; } ?> Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/ Share on other sites More sharing options...
jitesh Posted July 28, 2007 Share Posted July 28, 2007 <?php $query = "SELECT ID FROM products WHERE Username = '{$Username}'"; $result = mysql_query($query) or die(mysql_error()); while (list($ID) = mysql_fetch_row($result)) { echo $ID."<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-309406 Share on other sites More sharing options...
jitesh Posted July 28, 2007 Share Posted July 28, 2007 <?php $query = "SELECT ID FROM products WHERE Username = '{$Username}'"; $result = mysql_query($query) or die(mysql_error()); $i = 0; while (list($ID) = mysql_fetch_row($result)) { echo $i." ".$ID."<br>"; $i = $i + 1; } ?> Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-309407 Share on other sites More sharing options...
ballhogjoni Posted July 28, 2007 Author Share Posted July 28, 2007 htx for the reply. I just want to ECHO the number of rows. Meaning, if there is 10 rows, just ECHO the number 10, not each row. Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-309408 Share on other sites More sharing options...
jitesh Posted July 28, 2007 Share Posted July 28, 2007 <?php $query = "SELECT ID FROM products WHERE Username = '{$Username}'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)){ echo " Total Records ".mysql_num_rows($result); }else{ echo " No Record Found "; } ?> Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-309410 Share on other sites More sharing options...
ballhogjoni Posted July 28, 2007 Author Share Posted July 28, 2007 Thank you. This is what I figured out <?php $query = "SELECT ID FROM products WHERE Username = '{$Username}'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_num_rows($result); echo $row; ?> Its work perfectly, thank you for your help. Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-309413 Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 Don't retrieve more data than you need in queries - it only slows things down. If you just want a count, select the count, don't retrieve all the records then count them. <?php $query = "SELECT COUNT(*) FROM products WHERE Username = '$Username' "; $result = mysql_query($query) or die(mysql_error()); $count = mysql_result($result, 0, 0); Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-309456 Share on other sites More sharing options...
ballhogjoni Posted July 30, 2007 Author Share Posted July 30, 2007 thank you Link to comment https://forums.phpfreaks.com/topic/62150-while-loop/#findComment-310525 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.