从mysql中检索数据并使用php将其显示在表中

时间:2017-09-21 09:20:43

标签: php mysql

我正在尝试从数据库中检索所有数据并将其显示在表格中。但我无法做到这一点,面临一些问题。我收到了错误,

  

此页面无效。

     

localhost目前无法处理此请求。

这是我的代码,

<html>
<body>
<table style="width:100%">
  <tr>
    <th>Driverid</th>
    <th>Truckid</th> 
    <th>Imagecount</th>
    <th>Trainingstatus</th>
  </tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $driverid = $row["Driverid"];
    $truckid = $row["Truckid"];
    $imagecount = $row["Imagecount"];
    $trainingstatus = $row["Trainingstatus"];?>
<tr>
    <td><?php echo $driverid; ?></td>
    <td><?php echo $truckid; ?></td>
    <td><?php echo $imagecount; ?></td>
    <td><?php echo $trainingstatus; ?></td>
  </tr>
</table>
    <?php}
} else {
    echo "0 results";
}
$conn->close();
?>
</body>

</html>

3 个答案:

答案 0 :(得分:0)

请替换您的代码

来自

driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];

$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];

答案 1 :(得分:0)

请阅读有关如何在 PHP Manual 上在php中定义变量的部分,然后检查代码中定义的变量。

答案 2 :(得分:0)

你还没有开始表标签,也没有开始循环,主要部分是sdd空间<?php}<?php }

if ($result->num_rows > 0) {
echo '<table>';
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $driverid = $row["Driverid"];
    $truckid = $row["Truckid"];
    $imagecount = $row["Imagecount"];
    $trainingstatus = $row["Trainingstatus"];?>
<tr>
    <td><?php echo $driverid; ?></td>
    <td><?php echo $truckid; ?></td>
    <td><?php echo $imagecount; ?></td>
    <td><?php echo $trainingstatus; ?></td>
  </tr>
    <?php }
echo '</table>';
} else {
    echo "0 results";
}