php在html中显示mysql表

时间:2018-03-27 14:27:58

标签: php html mysql sql

我目前正在制作关于php和mysql的教程。我目前正在编写一个脚本,该脚本应该从sql数据库中读取数据并在html table中显示该数据。我有一个问题,即使用空字段生成表。它具有正确的字段数,但这些字段为空。

这是我的php脚本:

<?php
require "init.php";

$select = "select * from ScoresTest";
$sqlSel = mysqli_query($con,$select);
if(mysqli_num_rows($sqlSel)>0)
{

    echo("Works");
}
echo"<table border = '1'>
<tr>
<th>Username</th>
<th>Score</th>
</tr>";
while($row  = mysqli_fetch_row($sqlSel))
{

    echo "<tr>";
  echo "<td>" . $row['USERNAME'] . "</td>";
  echo "<td>" . $row['SCORES'] . "</td>";
  echo "</tr>";
}

echo"</table>";


?>

结果如下: Html table

我的表中的行数没问题,但字段是空的。为什么?

3 个答案:

答案 0 :(得分:1)

fetch_row() https://secure.php.net/manual/en/mysqli-result.fetch-row.php

上的手册

显示使用以下数组键$row[0], $row[1]

但是你正在使用列名。

在这种情况下,请使用fetch_assoc() https://secure.php.net/manual/en/mysqli-result.fetch-assoc.php

另外,请确保这些是实际名称,而不是在另一个字母案例中。

PHP的错误报告将(也)帮助您https://php.net/manual/en/function.error-reporting.php

注意:在* NIX上,VARvar是两种不同的动物。

您还需要在网络服务器上运行并使用正确的协议。

这对我们来说是未知的。

答案 1 :(得分:1)

另一种方法是将mysqli_fetch_row更改为mysqli_fetch_assoc

while($row  = mysqli_fetch_assoc($sqlSel))
{

    echo "<tr>";
    echo "<td>" . $row['USERNAME'] . "</td>";
    echo "<td>" . $row['SCORES'] . "</td>";
    echo "</tr>";
}

答案 2 :(得分:0)

在此代码中,

while($row  = mysqli_fetch_row($sqlSel))
{

  echo "<tr>";
  echo "<td>" . $row['USERNAME'] . "</td>";
  echo "<td>" . $row['SCORES'] . "</td>";
  echo "</tr>";
}

当然,如果您的行名称为$ row [&#39; USERNAME&#39;]?也许您的行名称是小写,或者您的字段名称不正确。 你也可以使用行的位置&#34;进行调试&#34;($ row [0],$ row [1])