0

我想在数组中回显多行,但它只输出一行:PHP

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
   while ($row1 = mysql_fetch_array($result)) {
     $fname1 = $row1['FName'];       
     $sname1 = $row1['SName'];
     }

$result2 = mysql_query($query);
   while ($row2 = mysql_fetch_array($result2)) {
     $fname2 = $row2['FName'];       
     $sname2 = $row2['SName'];
     }

HTML

<h2>First Name: <?php echo "$fname1"; ?></h2>
<h2>Second Name: <?php echo "$sname1"; ?></h2>

<h2>First Name: <?php echo "$fname2"; ?></h2>
<h2>Second Name: <?php echo "$sname2"; ?></h2>

但是当两者应该不同时,它会给我相同的输出。输出是:

名字:约翰第二名字:史密斯

名字:约翰第二名字:史密斯

当我希望输出为:

名字:约翰第二名字:史密斯

名字:鲍勃第二名字:马利

谁能帮我解决这个问题?\

数据库中的数据是:

User_ID| FName |SName| Category
1        John   Smith  Men
2        Bob    Marley Men
4

4 回答 4

2

while 循环一遍又一遍地覆盖你的变量,只输出最后一个结果,你必须将 echo 放在 while 中或将其保存到数组中,然后循环遍历数组

还必须“停止使用 mysql_ 函数”并更改为 PDO :)

您的代码的解决方案是

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
while ($row1 = mysql_fetch_array($result)) {
?> <h2>First Name: <?php echo $row1['FName']; ?></h2>
  <h2>Second Name: <?php echo $row1['SName']; ?></h2><?php       
}
于 2013-05-25T14:54:52.763 回答
1

使用数组,也只是跟踪你的代码。

PHP:

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
   $fname=array();
   $sname=array();

   for($i=0;$i<2 && ($row = mysql_fetch_array($result));$i++) {
     $fname[$i] = $row['FName'];       
     $sname[$i] = $row['SName'];
     }

HTML:

<h2>First Name: <?php echo "$fname[0]"; ?></h2>
<h2>Second Name: <?php echo "$sname[0]"; ?></h2>

<h2>First Name: <?php echo "$fname[1]"; ?></h2>
<h2>Second Name: <?php echo "$sname[1]"; ?></h2>
于 2013-05-25T15:10:52.030 回答
0
$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
while ($row1 = mysql_fetch_array($result)) {
 $fname1 = $row1['FName'];       
 $sname1 = $row1['SName'];
echo " <h2>First Name: $fname1</h2>";
echo "<h2>Second Name: $sname1</h2>";
 }
于 2013-05-25T14:59:15.977 回答
0

您正在覆盖您的值,因为您的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))
{
?>  <h2>First Name: <?=$row['FName'] ?></h2>
    <h2>Second Name: <?=$row['SName'] ?></h2><br /><?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);
    ?>  
    <h2>First Name: <?=$FName ?></h2>
    <h2>Second Name: <?=$SName ?></h2><br /><?php
}
于 2013-05-25T15:00:17.690 回答