简体   繁体   中英

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>";

     }
?>

尝试

SELECT DISTINCT category FROM #DB_Table

尝试使用选择不同

 select distinct category from your_table;

尝试这个

select DISTINCT category from your_table group by category;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM