Why is PHP not displaying all values correctly?

177
December 29, 2017, at 06:32 AM

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?

Answer 1

You are using $result 2 times on $result = mysqli_query($con, $sql) and in your foreach loop. I changed the one in the foreach loop to $results instead of $result.

Also try turning on error reporting using:

error_reporting(E_ALL);

Try using:

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

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>
Rent Charter Buses Company
READ ALSO
DATE_FORMAT in PHP query from MySQL error

DATE_FORMAT in PHP query from MySQL error

I need to select a date type field through a PHP queryThe problem suggests when I try to modify the value to the type d-m-Y

240
How to output a value (cost) based on a user selecting a range of dates in a form

How to output a value (cost) based on a user selecting a range of dates in a form

I'm building a villa booking system for a single property so that users can book directly on the websiteI'm struggling to find a way to calculate the total cost of the villa stay so that I can push it to my payment system (Stripe)

164
Too many database connection php

Too many database connection php

In my logoutphp endpoint I'm using 3 different classes

219
PHP 7.2 MCRYPT_BLOWFISH replacement? [duplicate]

PHP 7.2 MCRYPT_BLOWFISH replacement? [duplicate]

This question already has an answer here:

311