SNos SNos - 7 months ago 21
SQL Question

PHP - MYSQL - Avoid counting duplicated elements

I am trying to avoid to display the elements that have the same name.

For example my table is:

+----------+----------------------+
| name | category |
+----------+----------------------+
| AAA | Sport |
+----------+----------------------+
| BBB | City |
+----------+----------------------+
| CCC | Sport |
+----------+----------------------+
| DDD | Sun |
+----------+----------------------+
| EEE | Sport |
+----------+----------------------+
| FFF | Sun |
+----------+----------------------+


How can I get an array of
category
elements avoiding repetitions?

Output Array:

["Sport", "City", "Sun"]


My Code is:

<?php

$return_arr = array();

$sql="SELECT * FROM $DB_table";

$resultCat = $conn->query($sql);


while($row = $resultCat->fetch_assoc()) {

$category = $row['category'];
$row_array['category'] = $row['category'];

array_push($return_arr,$row_array);
echo "<option value=$category>$category</option>";

}
?>

Answer

try

SELECT DISTINCT category FROM #DB_Table
Comments