In the provided database structure (id, name, time, path, referer, ip), what SQL query can be used to display the most recent activity of each user who has been active in the last 15 minutes?
To display the most recent activity of each user who has been active in the last 15 minutes, we can use the following SQL query: ```sql SELECT id, name, MAX(time) AS last_activity FROM table_name WHERE time >= NOW() - INTERVAL 15 MINUTE GROUP BY id, name ``` This query selects the id, name, and the maximum time for each user who has been active in the last 15 minutes. It filters the results based on the time column and groups the results by id and name.
<?php
// Assuming you have already connected to the database
$query = "SELECT id, name, MAX(time) AS last_activity
FROM table_name
WHERE time >= NOW() - INTERVAL 15 MINUTE
GROUP BY id, name";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "User: " . $row['name'] . " | Last Activity: " . $row['last_activity'] . "<br>";
}
} else {
echo "No active users found in the last 15 minutes.";
}
mysqli_close($connection);
?>
Keywords
Related Questions
- What considerations should be taken into account when deciding between using PHP or JavaScript for dynamic content updates based on user interactions?
- In what situations would using an index.html file for redirection be beneficial in PHP development?
- How does the maximum execution time, memory limit, and max upload size affect PHP when uploading large files?