How can PHP be used to dynamically display event results on a website when a user clicks on a specific event?
To dynamically display event results on a website when a user clicks on a specific event, you can use PHP in combination with AJAX. The PHP script will fetch the event details from a database based on the event ID passed through AJAX, and then return the results back to the webpage for display without refreshing the page.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "events";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if event ID is set
if(isset($_GET['event_id'])) {
$event_id = $_GET['event_id'];
// Fetch event details from database
$sql = "SELECT * FROM events WHERE id = $event_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Event Name: " . $row["name"] . "<br>";
echo "Event Date: " . $row["date"] . "<br>";
echo "Event Location: " . $row["location"] . "<br>";
}
} else {
echo "No event found.";
}
}
$conn->close();
?>
Keywords
Related Questions
- In what situations is it advisable to use a different MySQL user with higher privileges to successfully execute PHP scripts for database operations?
- How can PHP developers ensure that their link converter code is efficient and effective?
- How can PHP functions be optimized to handle both GPIO control and text output without compromising code structure or readability?