How can PHP be used to count and display the number of entries for each user in a database table?

To count and display the number of entries for each user in a database table, you can use a SQL query to group the entries by user and then count the number of entries for each user. You can then fetch this data in PHP and display it accordingly.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to count entries for each user
$sql = "SELECT user_id, COUNT(*) as entry_count FROM entries GROUP BY user_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "User ID: " . $row["user_id"]. " - Entry Count: " . $row["entry_count"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>