数据表错误

时间:2016-12-07 13:55:34

标签: php jquery jquery-plugins datatables

我正在使用数据表来显示来自数据库的记录,使用PHP。

请参阅下面的代码;

<table class="table datatable-basic">
  <thead>
    <tr>
      <th> ID </th>
      <th> Name</th>
      <th>Address </th>
      <th class="text-center">Actions</th>
    </tr>
  </thead>
  <tbody>

  <?
  $result9023=mysql_query("SELECT * FROM hr_locations")or die('ERROR 315' );
  $num_rows = mysql_num_rows($result9023);

  for ($i = 1; $i <= mysql_num_rows($result9023); $i++) {
    $row = mysql_fetch_array($result9023);
    $location_id = $row ['id'];
    $name = $row ['location'];
    $address = $row ['address'];

    echo "         <tr>
        <td>$location_id</td>
        <td>$name</td>
        <td>$address</td>
        <td class='text-center'>Edit</td>
      </tr>
    "; } ?>
  </tbody>  
</table>

表格显示正确,填充的数据应该是正确的,但是当页面加载时我收到以下错误。

Error

我看过https://datatables.net/tn/4然而它没有多大意义?

1 个答案:

答案 0 :(得分:3)

由于大多数MySQL数组以0开头,所以:

a:hover ~ div

设置for ($i = 0; $i < $num_rows; $i++) { 然后声明$i = 0必须小于行数(如果你有4,你将获得0,1,2,3(4行,正确索引)。而不是再次计算行,只需使用已为计数设置的变量。

你的代码实际上可能做得太多了。不要使用不必要的for循环,只需使用while:

$i

通过这种方式,您可以确保捕获从查询返回的每一行,而不会像在尝试使用for循环和while($row = mysql_fetch_array($result9023)){ $location_id = $row ['id']; $name = $row ['location']; $address = $row ['address']; echo " <tr> <td>$location_id</td> <td>$name</td> <td>$address</td> <td class='text-center'>Edit</td> </tr> "; } ?> 时那样重复或跳过行。