What potential issue arises when trying to display the last 4 played titles from an online radio using PHP and a database?
When trying to display the last 4 played titles from an online radio using PHP and a database, one potential issue that may arise is ensuring that the titles are displayed in the correct order based on when they were played. To solve this issue, you can store a timestamp along with each title in the database and then retrieve the titles ordered by the timestamp in descending order.
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "radio_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve last 4 played titles ordered by timestamp
$sql = "SELECT title FROM played_titles ORDER BY timestamp DESC LIMIT 4";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Title: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();