What are some best practices for efficiently displaying unique data from a database in PHP?
When displaying unique data from a database in PHP, it is best to utilize SQL queries to retrieve only the distinct values. This can help reduce the amount of data fetched from the database and improve efficiency. Additionally, using proper indexing on the database columns can also speed up the retrieval process.
// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");
// Query to select distinct values from a specific column
$query = "SELECT DISTINCT column_name FROM table_name";
// Execute the query
$result = $connection->query($query);
// Display the unique data
while ($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
// Close the connection
$connection->close();