【发布时间】:2019-02-13 19:57:49
【问题描述】:
我正在尝试将数据库中的信息显示到 html 页面,但它只显示“连接成功”的消息。数据库的名称是“admin”,该数据库中的表称为“users”。我不知道如何绕过这条消息,只显示我创建的表格。
索引页(index.php):
<?php
include_once('connection.php');
$query="select * from users";
$result=mysql_query($query);
?>
<!DOCTYPE html>
<html>
<title>
<head> Fetch Data Frome Database</head>
</title>
<body>
<table align="center" border="1px" style="width:250px; line-height: 30px;">
<tr>
<th colspan="4"><h2>Account Record</h2></th>
</tr>
<t>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</t>
<?php
while($rows=mysql_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $rows['ID'];?></td>
<td><?php echo $rows['username'];?></td>
<td><?php echo $rows['password'];?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
连接页面(connection.php):
<?php
//Include your own values for username, password and dbase name
$host = "localhost";
$username = "root";
$password = "root";
$dbname = "admin";
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
【问题讨论】:
-
您使用 mysqli 连接,但在查询中使用了 mysql。
-
谢谢。尽管这有助于将表格实际显示到网页上,但表格中的结果并未显示。我将查询 sql 更改为 sqli,将 while 循环更改为 sqli。我是否缺少任何实际显示结果的内容?
-
-
我已经把我的编码改成了上面的^^^。这现在显示了表格,以及我在数据库中拥有的 3 组数据的 3 个插槽。仍然没有将其从数据库中提取出来
-
警告:不要使用在 PHP 7 中删除的过时的
mysql_query接口。PDO is not hard to learn 之类的替代品和PHP The Right Way 之类的指南有助于解释最佳实践.这里的参数是 NOT properly escaped 并且在这段代码中有严重的 SQL injection bugs。转义任何和所有用户数据,尤其是来自$_POST或$_GET。
标签: php html mysql database fetch