How can you prevent duplicate values from being displayed in a PHP query result?
When running a PHP query, you can prevent duplicate values from being displayed by using the DISTINCT keyword in your SQL query. This keyword ensures that only unique values are returned in the result set. By including DISTINCT in your query, you can eliminate any duplicate values that may be present in the database table.
$query = "SELECT DISTINCT column_name FROM table_name";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row['column_name'] . "<br>";
}
}
Keywords
Related Questions
- How can the structure of a PHP script be optimized to avoid conflicts with sending headers and starting sessions?
- What alternative approaches can be used to dynamically change the background color of a cell in a table without using IF statements in PHP?
- How can a PHP beginner output a variable multiple times in a string?