Jump to content

while loop?


ballhogjoni

Recommended Posts

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

<?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

<?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

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

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

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.