gpt4 book ai didi

php - 我正在尝试将 MySQL 数据库中的数据显示到 HTML 表上,但没有注册列字段

转载 作者:行者123 更新时间:2023-11-29 10:56:13 24 4
gpt4 key购买 nike

我是 PHP 编码新手,不明白哪里出了问题,我试图将 MySQL 数据库中的数据显示到 HTML 表上,但没有注册列字段,不断出现通知: undefined index :首先,注意: undefined index :最后等。如何定义我的列字段?

编辑:这里是我的数据库图片和我收到的错误代码的链接:http://imgur.com/E5uohjr http://imgur.com/BGDn9SQ

这是我的代码:

</head>

$con = mysqli_connect('localhost', 'root', '', 'form_database') or die("Can not connect: " . mysqli_error());

$result = mysqli_query($con,"SELECT first, last, phone, class FROM form_submissions");

echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";

while($row = mysqli_fetch_assoc($results)){
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}

echo "</table>";

mysqli_close($con);

?>
</body>

最佳答案

在显示结果之前检查是否有结果。

<?php
// Create connection
$con = mysqli_connect('localhost', 'root', '', 'form_database');
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM form_submissions";
$result = mysqli_query($con, $sql);

//check if you get any results
if (mysqli_num_rows($result) > 0) {
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";

// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No results found";
}

mysqli_close($con);

关于php - 我正在尝试将 MySQL 数据库中的数据显示到 HTML 表上,但没有注册列字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42977088/

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