我有一个文件 uploadpostinfo.php,
<html>
<head>
</head>
<body>
<?php
error_reporting(E_ALL);
$db = "pickeqco_postinfo";
$link = mysqli_connect($host, $user, $pass, $db);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The $db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
if (!mysqli_select_db($link, $db)) {
echo "Database not selected";
}
$sql = "SELECT id, topic, bullet1, bullet2, expl, source FROM postinformation";
$res = $list->query($sql);
if ($res->num_rows > 0) {
echo "<table><tr><th>ID</th><th>topic</th></tr>";
// output data of each row
while($row = $res->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["topic"]." ".$row["bullet1"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
//echo nl2br("\n\n$topic\n$bullet1\n$bullet2\n$expl\n$source\n\n");
?>
</body>
</html>
我想从数据库表中获取值并将其插入到我的index.html 文件中。 uploadpostinfo.php和index.html在同一个目录下,但是是不同的文件。
<form action="uploadpostinfo.php" method="post">
<input name="submit" type="submit" value="getdata"/>
</form>
有人可以帮我将 mysql 表数据放入我的 html 代码中吗?
最佳答案
您正在传递结果变量,但数据位于 $res 中,因此请更改
while($row = $result->fetch_assoc()) {
到
while($row = $res->fetch_assoc()) {
关于php - 将 MYSQL 表数据传递到 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861511/