我知道如何使用html添加表格,但是当涉及到如何将SQL数据添加到表格时,我不知道从哪里开始。我所看到的一切都向我展示了如何使用 PHP 从 SQL 创建和修改表,但这不是我所需要的。那么,我从哪里以及如何开始从 SQL 添加数据呢?
这就是我目前所拥有的
<html>
<h2> MySQL Assignment </h2>
<table border=2>
<tr>
<th>person</th>
<th>name</th>
<th>cost</th>
</tr>
</table>
</html>
<?php
//error_reporting(0);
require 'connect.php';
if($result = $db->query("SELECT * FROM store")){
if($count = $result->num_rows) {
echo '<p>', $count, '</p>';
while($row = $result->fetch_object()) {
echo $row->person, ' ', $row->name,
' ', $row->cost, '<br>';
}
}
}
使用目前为止的代码,并假设它成功检索数据,将检索 SQl 数据的代码移动到表的中心:
<?php
//error_reporting(0);
require 'connect.php';
?>
<html>
<h2> MySQL Assignment </h2>
<table border=2>
<tr>
<th>person</th>
<th>name</th>
<th>cost</th>
</tr>
<?php
if($result = $db->query("SELECT * FROM store")){
if($count = $result->num_rows) {
while($row = $result->fetch_object()) {
echo '<tr><td>$row->person, '</td><td>', $row->name,'</td><td>', $row->cost,'</td></tr>, "\n";
}
}
}
?>
</table>
</html>
这不一定是生产环境中的最佳方法,但它应该可以帮助您入门。
注意:这是你的代码,我还没有测试过。如果这没有按您的预期工作,请在寻求更多帮助之前彻底调试它。