您正在覆盖您的值,因为您的while循环每次都在迭代每一行。尝试这个:
$query = "SELECT * FROM user WHERE Category = 'Men'";
// mysql_ = bad. mysqli_ = good!
$result = mysql_query($query);
$row1 = mysql_fetch_array($result);
$fname1 = $row1['FName'];
$sname1 = $row1['SName'];
// using the same $result.
$row2 = mysql_fetch_array($result);
$fname2 = $row2['FName'];
$sname2 = $row2['SName'];当然,正如其他地方所述,如果您的表中有两个以上的项目并且想要输出中的每个项目,那么此解决方案将无效。如果是这种情况,你会想要这样的东西:
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
?>
First Name: =$row['FName'] ?>
Second Name: =$row['SName'] ?>
<?php}或者,根据您的需要:
$result = mysql_query($query);
$men = array();
while($row = mysql_fetch_array($result))
{
$men[] = $row;
}
// then later in the script
foreach($men as $man)
{
// extract takes all of the array keys and turns them into local variables.
// just make sure you read the warnings in the docs:
// http://php.net/manual/en/function.extract.php
extract($man);
?>
First Name: =$FName ?>
Second Name: =$SName ?>
<?php}