【发布时间】:2015-08-20 11:48:09
【问题描述】:
我想在 html 的表格中显示数据库中的内容。我在互联网上的某个地方找到了这个示例,并按照整个指南进行操作,直到我到达那里。它显示表和所有信息,但将数据库中的信息放入“$data[1]”和“$data[0]”中。 感谢它提前
<html>
<head>
<title>Search data</title>
</head>
<body>
<table>
<tr>
<td align="center">EMPLOYEES DATA</td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td>NAME</td>
<td>ADDRESS</td>
</tr>
<?php
//the example of searching data
with the sequence based on the field name
//search.php
mysql_connect("localhost","root","");//database connection
mysql_select_db("employees");
$order = "SELECT * FROM data_employees ORDER BY name";
//order to search data
//declare in the order variable
$result = mysql_query($order);
//order executes the result is saved
//in the variable of $result
while($data = mysql_fetch_row($result)){
echo("<tr><td>$data[1]</td><td>$data[0]</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>
【问题讨论】:
-
如果可以的话,你应该stop using
mysql_*functions。它们不再被维护并且是officially deprecated。改为了解 prepared statements,并考虑使用 PDO,it's really not hard。您还应该在查询中添加错误检查,例如or die(mysql_error())。或者您可以在当前的错误日志中找到问题。 -
"它只是放入 "$data[1]" 和 "$data[0]"。" - 文件是
.php扩展名吗?服务器运行?这是代码的一部分吗?with the sequence based on the field name不应该在那里。 -
安装网络服务器,或者如果在托管站点上,请将您的文件重命名为
file.php。那里;固定。 -
这些 cmets 是否正在被阅读 Sam? - @JayBlanchard
-
这就是我从一开始就告诉你的
with the sequence based on the field name那行。你为什么接受下面的答案?那没有解决它,怀疑。