我不明白为什么这不起作用,我已经坚持了很长时间并尝试了很多不同的替代方案,但它就是不打印数据库中的数据。
目前我只是想打印id,但最终我想打印数据库中的大部分数据(不包括哈希)。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Staroids Leaderboard</title>
</head>
<body>
<table border=1px>
<thead>
<tr>
<td>name</td>
<td>score</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "password");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("staroids");
$results = mysql_query("SELECT id FROM scores");
while($row = mysql_fetch_array($results)) {
$name = $row['id']
?>
<tr>
<td><?php echo '$name'?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
下图显示了它在 html 中的样子:
此图显示了本地主机中的数据库,如您所见,有很多数据,但似乎没有一个名称打印出来?!
最佳答案
在可能的地方改正你的语法
$name = $row['id']; //Put ; here
<?php echo $name;?> //Remove quotes and put ;
从数据库中选择名字,然后就可以得到名字,应该是
$results = mysql_query("SELECT id,name FROM scores");
while($row = mysql_fetch_array($results)) {
$name = $row['name'];
?>
<td><?php echo $name;?></td>
并且不要使用 mysql_*
函数,因为它们已被弃用。而是使用 mysqli_*
函数或 PDO
语句。
正如@Nedstark 所说,使用try die(mysql_error());
来查找有关 mysql 错误的错误。
关于php - 从 SQL 数据库中选择数据并在表中显示不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18099675/