php - 使用 PHP 在 HTML 页面中显示 SQL 表内容

标签 php html mysql

我有一个 SQL 表,我想在 HTML 页面中显示它。 我的代码显示一个表格,但内容是空的,尽管我的表格充满了数据。

    <?php
        ini_set('display_errors', 'On');
        error_reporting(E_ALL);

        $database = 'karin';
        $connect = mysql_connect('localhost','root', 'karin');
        if (!$connect) {
            die(mysql_error());
        }
        if (!mysql_select_db($database))
            die("Can't select database");

        $results = mysql_query("SELECT * FROM service_messages");
        if (!$results) {
            die("Query to show fields from table failed");
        }


        while($row = mysql_fetch_array($results)) {
        ?>
            <tr>
                <td><?php echo $row['partner']?></td>
                <td><?php echo $row['GUID']?></td>
                <td><?php echo $row['serviceName']?></td>
                <td><?php echo $row['serviceID']?></td>
                <td><?php echo $row['countries']?></td>
                <td><?php echo $row['rate']?></td>
                <td><?php echo $row['ips']?></td>
                <td><?php echo $row['callback']?></td>
            </tr>

        <?php
        }
        ?>
        </tbody>
        </table>
</body>

为什么不显示表格内容? 谢谢!

最佳答案

1).不要使用mysql_*。请使用 PDO 或 MySQLi。

2).我没有找到<table> (表起始标记)代码中的任何位置。

3).使用mysql_fetch_assoc而不是mysql_fetch_array .

这是更新后的代码。

    $database = 'karin';
    $connect = mysql_connect('localhost','root', 'karin');
    if (!$connect) {
        die(mysql_error());
    }
    if (!mysql_select_db($database))
        die("Can't select database");

    $results = mysql_query("SELECT * FROM service_messages");
    if (!$results) {
        die("Query to show fields from table failed");
    }
     ?>
    <table>
    <?php
    while($row = mysql_fetch_assoc($results)) {
    ?>
        <tr>
            <td><?php echo $row['partner']?></td>
            <td><?php echo $row['GUID']?></td>
            <td><?php echo $row['serviceName']?></td>
            <td><?php echo $row['serviceID']?></td>
            <td><?php echo $row['countries']?></td>
            <td><?php echo $row['rate']?></td>
            <td><?php echo $row['ips']?></td>
            <td><?php echo $row['callback']?></td>
        </tr>

    <?php
    }
    ?>
    </tbody>
    </table>

关于php - 使用 PHP 在 HTML 页面中显示 SQL 表内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594544/

相关文章:

javascript - 在 div 中显示 json html 数据

javascript - 不使用 App.vue 时带有 Vite 的 Vue 3 渲染空白页面

html - 下拉栏 float 的右/下。整个网格移动一个悬停在它上面

php - 我如何添加一条显示 "$user_id Deleted"或 "$user_id not found?"的消息

mysql - 在 MySQL 数据库中的所有类似表(包含相同架构)上运行 ALTER TABLE

php cs fixer,如何运行有风险的规则?

php - Laravel 通过 excel 上传图片

javascript - 使用 javascript 或 PHP 根据输入自动减去数字,无需单击按钮

php - Mysqli 抛出 "Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given"

MySQL SHA2 函数似乎不起作用