How can the data from the posts table be retrieved and displayed in an HTML table format, including user-specific information?
To retrieve and display data from the posts table in an HTML table format, you can use PHP to fetch the data from the database and then loop through the results to create rows in the HTML table. You can include user-specific information by joining the users table with the posts table using a common user_id field.
<?php
// Assuming you have already established a database connection
// Query to retrieve posts data including user-specific information
$sql = "SELECT posts.*, users.username FROM posts
INNER JOIN users ON posts.user_id = users.id";
$result = mysqli_query($conn, $sql);
// Check if there are any results
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr><th>Title</th><th>Content</th><th>Author</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['content'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No posts found";
}
// Close the database connection
mysqli_close($conn);
?>