在表中显示记录 [英] display records in table

查看:55
本文介绍了在表中显示记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个PHP项目遇到了一些麻烦,我试图从MySQL数据库(具有3条记录)中获取数据并将其显示在表格中.问题是似乎只显示记录2和3,它跳过了第一个记录.请查看我的代码并在下面显示.

I am having some trouble with my first PHP project, I am trying to get the data from MySQL database (Has 3 records) and display it in tables. Problem is it only seem to display records 2 and 3, it skips the 1st record. Please see my code and display below.

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM unitstats");

while($row = mysqli_fetch_array($result)) {
  echo "<table border='1' style='color:white'>
  <tr>
  <th>ID</th>
  <th>Name</th>
  </tr>";

  while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "</tr>";
  }
  echo "</table>";
}

推荐答案

您正在使用两个while循环,而不必要使用以下代码

you are using two while loop which is unnecessary use following code

if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

      echo "</table>";


echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM unitstats");

while($row = mysqli_fetch_array($result))
  {


echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "</tr>";
}
  echo "</table>";

这篇关于在表中显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆