How can PHP be used to query a MySQL database for current events based on the current time?
To query a MySQL database for current events based on the current time using PHP, you can use the NOW() function in your SQL query to get the current timestamp and compare it with the event start and end times in your database. This way, you can retrieve events that are currently happening or scheduled to start in the future.
<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Query for events based on current time
$query = "SELECT * FROM events WHERE start_time <= NOW() AND end_time >= NOW()";
$result = $mysqli->query($query);
// Loop through results
while ($row = $result->fetch_assoc()) {
echo $row['event_name'] . " is happening now or will start soon. <br>";
}
// Close database connection
$mysqli->close();
?>