douli4337 2017-12-28 19:41
浏览 57
已采纳

为什么PHP没有正确显示所有值?

Unfortunately my code isn't working quite well.

The source code:

<!DOCTYPE html>
<html>
<head>
    <title>Query data from News database and display in table</title>
    <meta charset="UTF-8">
    <meta name="description" content="" />
    <meta name="author" content="WRBikAir" />
    <meta name="keywords" content="" />
<style> 
    table, th, td{
        border: 1px solid black;
    }
</style>
</head>

<body>
    <?php
        error_reporting(E_ALL);
        echo "Test 1";
        $con = mysqli_connect("mysql.hostinger.com","u441817146_admin","CBGApp","u441817146_cbg");
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $sql = "SELECT * FROM News";
        if ($result = mysqli_query($con, $sql)) {
            echo "<table>";
            while($row = $result->fetch_assoc()) {
                $r = json_encode($row);
                echo "<tr><td>" . $r['NID'] . "</td><td>" . $r['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $r['timestamp'] . "</td></tr>";
            }
            echo "</table>";
        } else {
            echo "no result.";
        }
        mysqli_close($con);
        echo "2";
    ?>
</body>
</html>

Everything works fine, except of the output of the NID, headline and timestamp. There are all '{'. Does it mean, that there is now value? because if I simply print them out (encoded of course) there are values e.g.:

{"NID":"1","headline":"Testartikel 2","text":"test test test","timestamp":"15.11.2017, 18:13"}

Does somebody knows a solution?

  • 写回答

2条回答 默认 最新

  • dsbc80836 2017-12-28 21:34
    关注

    For everybody who need the working answer.

    Thank you to MasterOfCoding, GrumpyCrouton, Paul Spiegel and prodigitalson. Special thanks to tadman for the insider information.

    Now the code:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Query data from News database and display in table</title>
            <meta charset="UTF-8">
            <meta name="description" content="" />
            <meta name="author" content="WRBikAir" />
            <meta name="keywords" content="" />
    
            <style> 
                table, th, td{
                    border: 1px solid black;
                }    
            </style>
        </head>
        <body>
            <?php
                error_reporting(E_ALL);
                echo "Test 1";
                $con = mysqli_connect("...","...","...","...");
                if (mysqli_connect_errno()) {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
    
                $sql = "SELECT * FROM News";
                if ($result = mysqli_query($con, $sql)) {
                    echo "<table>";
                    while($row = $result->fetch_assoc()) {
                        echo "<tr><td>" . $row['NID'] . "</td><td>" . $row['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $row['timestamp'] . "</td></tr>";
                    }
                    echo "</table>";
                } else {
                    echo "no result.";
                }
                mysqli_close($con);
                echo "2";
            ?>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码