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; } ?> Quote Link to comment 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>"; } ?> Quote Link to comment 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; } ?> Quote Link to comment 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. Quote Link to comment 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 "; } ?> Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted July 30, 2007 Author Share Posted July 30, 2007 thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.