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
Share on other sites

<?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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.