MySQL php select 语句使用 WAMP 服务器

MySQL php select statement using WAMP server

我正在尝试在我的 html 页面中显示数据库 table,但似乎无法正常工作。 (下面的代码):

<!DOCTYPE html>
<html>
<body>

<h1> Table </h1>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Library";

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

$sql = "SELECT * FROM `books` ORDER BY `Category` ASC ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> id: ". $row["ISBN"]. " Name: ". $row["firstname"]. " - Author: " . $row["BookAuthor"]. " Category: " . $row["Category"]. " - Quantity: " . $row["Quantity"]. " - Price: " . $row["Price"]. "<br>";
     }
} else {
     echo "0 results";
}

$conn->close();
?> 

</body>
</html>

我正在使用 WAMP 服务器,"books" table 位于名为 Library 的数据库中。但是 table 不显示,我得到的只是显示 php 代码。

知道哪里出了问题吗?

谢谢!

  1. 你的 WAMP 运行 是吗? (检查系统托盘图标。它应该是绿色的)
  2. 您是否弄乱了 PHP 或 Apache 配置?
  3. 文件扩展名是.php吗?必须的。
  4. 其他PHP显示正常吗?