php - 从 SQL 数据库中选择数据并在表中显示不起作用

标签 php mysql sql html-table

我不明白为什么这不起作用,我已经坚持了很长时间并尝试了很多不同的替代方案,但它就是不打印数据库中的数据。

目前我只是想打印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 中的样子:

enter image description here

此图显示了本地主机中的数据库,如您所见,有很多数据,但似乎没有一个名称打印出来?!

enter image description here

最佳答案

在可能的地方改正你的语法

$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/

相关文章:

php - 将用户输入与 PHP 数组进行比较

Mysql按时间间隔分组优化

SQL : Select a table name containing specific string

php - 如何在PHP中获取客户端IP地址

php - codeigniter nodejs 和 nowjs 集成

php - 无法使用 php 删除项目符号点 ' • '

mysql - 从MySql生成的sql脚本理解表关系

php - 从 PHP 生成转储文件

mysql - mysql IN 子句返回空值

mysql - 将两个查询合并为一个查询以挑选相关列