How can PHP developers optimize their code to efficiently display unique entries from a database?
To efficiently display unique entries from a database in PHP, developers can use the DISTINCT keyword in their SQL query to retrieve only unique values. Additionally, they can optimize their code by using proper indexing on the database columns that are being queried for unique entries.
// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");
// Query to retrieve unique entries from a specific column
$query = "SELECT DISTINCT column_name FROM table_name";
// Execute the query
$result = $connection->query($query);
// Display the unique entries
while ($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
// Close the database connection
$connection->close();