Jump to content


Photo

query displaying same row over and over


  • Please log in to reply
1 reply to this topic

#1 SF23103

SF23103

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts

Posted 27 April 2013 - 12:36 AM

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



#2 trq

trq

    Advanced Member

  • Administrators
  • 31,034 posts
  • LocationSydney, Australia.

Posted 27 April 2013 - 03:28 AM

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.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com