mardi 11 mai 2021

Using PHP to display data from a database in HTML in date order

I want to be able to display each post in a separate text box on my page sorted by date.

I have a database set up and the connection set up in PHP.

The table contains the following:

post_id (primary key) - int user_id (foreign key) - int post_contents - text post_date - datetime

I have a basic knowledge of SQL but am unsure how to actually display the data.

I had thought of something like this to get the data but am unsure how to use it now.

$sql = "SELECT * FROM tbl_posts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["user_id"] . $row["post_contents"]. " " . $row["post_date"];
    }
} else {
    echo "0 results";
}

Any help would be much appreciated.




Aucun commentaire:

Enregistrer un commentaire