How can the SQL query be optimized to display only one category for each image in the gallery?
The issue can be solved by using the DISTINCT keyword in the SQL query to retrieve only unique categories for each image in the gallery. This will ensure that each image is associated with only one category, eliminating duplicates in the result set.
$query = "SELECT DISTINCT image_id, category FROM gallery_table";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo "Image ID: " . $row['image_id'] . " | Category: " . $row['category'] . "<br>";
}
} else {
echo "No results found.";
}
Related questions
-
self JOIN MySQL database table
-
What are potential bugs to look out for when applying styles across multiple worksheets in PHPExcel?PHPExcel styling multiple worksheets
-
PayPal API PHP subscriptions billing plans