2016-04-20 115 views
0

我試圖避免顯示具有相同名稱的元素。PHP - MYSQL - 避免計數重複元素

例如我的表是:

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

我怎樣才能得到category元素避免重複的陣列?

輸出數組:

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

我的代碼是:

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

    } 
?> 
+0

'從t' –

+0

可能重複選擇不同類別[如何從MySQL表不同的記錄?(http://stackoverflow.com/questions/6109758/how-to-get -distinct記錄,從MySQL的表) –

回答

0

嘗試

SELECT DISTINCT category FROM #DB_Table 
0

嘗試使用SELECT DISTINCT

select distinct category from your_table; 
0

試試這個

select DISTINCT category from your_table group by category;