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

时间:2017-12-28 19:41:32

标签: php html mysql

不幸的是我的代码效果不佳。

源代码:

<!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>

除了NID,标题和时间戳的输出之外,一切正常。有所有'{'。这是否意味着现在有价值?因为如果我只是将它们打印出来(当然是编码的)就有值,例如:

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

有人知道解决方案吗?

2 个答案:

答案 0 :(得分:0)

您在$result和foreach循环中使用$result = mysqli_query($con, $sql) 2次。我将foreach循环中的那个更改为$results而不是$result

还可以尝试使用以下方式启用错误报告:

error_reporting(E_ALL);

尝试使用:

<!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
        echo "Test 1";
        $con = mysqli_connect(CENSORED (but working in other classes fine);
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $sql = "SELECT * FROM News";
        if ($result = mysqli_query($con, $sql)) {
            $resultArray = array();
            $tempArray = array();
            while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($resultArray, $tempArray);
            }
            echo "<table>";
            foreach ($resultArray as $results) {
                $r = json_encode($results);
                echo "<tr><td>" . $results['headline'] . "</td><td>" . $results['text'] . "</td></tr>";
            }
            echo "</table>"
        }
        mysqli_close($con);
        echo "2";
    ?>
</body>
</html>

答案 1 :(得分:0)

对于每个需要工作答案的人。

感谢MasterOfCoding,GrumpyCrouton,Paul Spiegel和prodigitalson。 特别感谢tadman的内幕消息。

现在代码:

<!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>