How can PHP be used to automatically play a sound when there is a change detected in a database?
To automatically play a sound when a change is detected in a database, you can use PHP in conjunction with AJAX to periodically check for updates in the database. When a change is detected, PHP can send a signal to the front end to play a sound using JavaScript.
<?php
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check for changes in the database
$query = "SELECT * FROM table";
$result = $conn->query($query);
if($result->num_rows > 0){
// Change detected, send signal to play sound
echo "<script>new Audio('sound.mp3').play();</script>";
}
?>
Keywords
Related Questions
- What are best practices for handling line breaks and platform dependencies in PHP code to ensure consistency and compatibility?
- How can excessive whitespace affect the functionality of PHP code?
- What are the best practices for organizing and calling functions in PHP scripts to avoid errors and improve readability?