gpt4 book ai didi

php - 使用 PhP 连接到 HTML 的 MySQL 表

转载 作者:行者123 更新时间:2023-11-29 20:52:07 26 4
gpt4 key购买 nike

当我尝试在 html 页面中使用“href”调用 php 文件时,出现错误:

“解析错误:语法错误,第 43 行 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\1.php 中出现意外的 '>'”。

我不知道第 43 行出了什么问题,代码如下:

<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>

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

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

$sql = "SELECT cod, rated_power, max_output_power, output_voltage, generator_type, rotor_diameter, turbine_weight, design_lifetime, price FROM turbine_specs";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Cod</th>
<th>Rated Power</th>
<th>Maximum Output Power</th>
<th>Output Voltage</th>
<th>Generator Type</th>
<th>Rotor Diameter</th>
<th>Turbine Weight</th>
<th>Design Lifetime</th>
<th>Price</th>
</tr>
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>" . $row["cod"]. "</td>
<td>" . $row["rated_power"]. "</td>
<td>" . $row["max_output_power"]. "</td>
<td>" . $row["output_voltage"]. "</td>
<td>" . $row["generator_type"]. "</td>
<td>" . $row["rotor_diameter"]. "</td>
<td>" . $row["turbine_weight"]. "</td>
<td>" . $row["design_lifetime"]. "</td>
<td>" . $row["price"]. "</td></tr>

}
echo "</table>";
} else {
echo "0 results";
}

$conn->close();
?>

</body>
</html>

我将包含此代码的文件作为“.php”文件保存到我的“localhost”文件夹中。

Image with containing titles of my mysql table

PS:抱歉,如果我的英语不好。

PS2:我是初学者。

最佳答案

在第 39 行,您需要用双引号结束。

 echo "<table>
<tr>
<th>Cod</th>
<th>Rated Power</th>
<th>Maximum Output Power</th>
<th>Output Voltage</th>
<th>Generator Type</th>
<th>Rotor Diameter</th>
<th>Turbine Weight</th>
<th>Design Lifetime</th>
<th>Price</th>
</tr>"; //you're missing the double quotation mark here, that's why you're getting the error.

关于php - 使用 PhP 连接到 HTML 的 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37951948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com