简体   繁体   中英

retrieve data from mysql and display it in a table using php

I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,

This page isn't working.

localhost is currently unable to handle this request.

Here is my code,

<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>

please replace your code

from

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

to

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

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

you have not started table tag, also not in loop, and main part is sdd space <?php} to <?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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM