我試圖避免顯示具有相同名稱的元素。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>";
}
?>
'從t' –
可能重複選擇不同類別[如何從MySQL表不同的記錄?(http://stackoverflow.com/questions/6109758/how-to-get -distinct記錄,從MySQL的表) –