Jump to content

query displaying same row over and over


SF23103

Recommended Posts

I have a table where I'm trying to display all rows where field5="Brand One".  For some reason it's printing the same row over and over instead of the different rows.  In other words it's doing this:

 

1/1/2012    A     Bob Smith    $3.99     Brand One

1/1/2012    A     Bob Smith    $3.99     Brand One

1/1/2012    A     Bob Smith    $3.99     Brand One

1/1/2012    A     Bob Smith    $3.99     Brand One

 

INSTEAD OF:

 

1/1/2012    A     Bob Smith    $3.99     Brand One

1/4/2013    B     John Smith    $4.99     Brand One

1/7/2013    C     Bob Johnson    $6.99     Brand One

1/8/2013    Q     George Smitherson    $8.99     Brand One

<?php
$username="XXXX";
$password="XXXX";
$database="XXXX";
$servername="XXXX";

mysql_connect($servername,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM `list` WHERE Field5 = 'brand one'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>

Brand One:

<table>

<?php
$i=0;
while ($i < $num) {

$list_result1=mysql_result($result,0,"field1"); //Date
$list_result2=mysql_result($result,0,"field2"); //Letter
$list_result3=mysql_result($result,0,"field3"); //Name
$list_result4=mysql_result($result,0,"field4"); //Price
$list_result5=mysql_result($result,0,"field5"); //Brand

?>
<tr><td><?php echo $list_result1; ?></td><td><?php echo $list_result2; ?></td><td><?php echo $list_result3; ?></td><td><?php echo $list_result4; ?></td><td><?php echo $list_result5; ?></td></tr>
   
<?php              
$i++;
}
?>
</table>

Any ideas?? :-)

Link to comment
Share on other sites

The second argument to mysql_result is the row. Your giving it 0 over and over again. Your not using $i anywhere.

 

Having said that, why are you using mysql_result at all? The documentation clearly states:

 

you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result().

On top of that, the mysql extension has been deprecated. Any new code should be written using PDO or MySQLi instead.

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.