如何从表打印数据?

时间:2013-08-14 23:53:23

标签: php mysql

为什么以下代码只打印一个条目:

$result = mysqli_query($con,"SELECT * FROM verifications WHERE user='seriot'");
$row = mysqli_fetch_array($result);
echo date(DATE_RFC822);
echo "<br>";
echo "<br>";
echo "<b>".$row['firstname'] . " " . $row['lastname']."</b>";
echo "<br>";
while($row = mysqli_fetch_array($result)){
   echo $row['filepath']." ".$row['type']; 
   echo "<br>";
}

虽然此代码将打印所有条目:

while($row = mysqli_fetch_array($result)){
  echo $row['filepath']." ".$row['type']; 
  echo "<br>";
}

我正在尝试打印名字和姓氏,然后打印所有文件路径和类型条目,直到下一个名称。

谢谢你, 凯文

2 个答案:

答案 0 :(得分:2)

你可以像这样使用它:

$result = mysqli_query($con,"SELECT * FROM verifications WHERE user='seriot'");
echo date(DATE_RFC822), '<br><br>';
$name = '';
while($row = mysqli_fetch_array($result))
{
    $current = $row['firstname'] . ' ' . $row['lastname'];
    if (empty($name) || $name != $current)
    {
        echo '<b>', $current, '</b><br>';
        $name = $current;
    }
    echo $row['filepath'], ' ', $row['type'], '<br>'; 
}

我们发起$name为空,然后我们将最后匹配的名称保存到$name,如果$current$name不同,我们会再次打印。

答案 1 :(得分:0)

你需要循环$ row ['firstname']和$ row ['lastname']来获取数组中的所有结果。像这样:

      $row = mysqli_fetch_array($result);
      while($row = mysqli_fetch_array($result)){
      echo "<b>".$row['firstname'] . " " . $row['lastname']."</b>";
      echo "<br>";
      echo $row['filepath']." ".$row['type']; 
      echo "<br>";
      }

上面的代码只会显示mysql_fetch_array

中的最后一个结果
         $row = mysqli_fetch_array($result);
         echo date(DATE_RFC822);
         echo "<br>";
         echo "<br>";
         echo "<b>".$row['firstname'] . " " . $row['lastname']."</b>";
         echo "<br>";