2016-03-04 30 views
0

在一個數據庫中,我有一個表tcastatus列。這可能是new, process, completed 當我查詢此表SELECT status, count(*) FROM tca GROUP BY status找出有一定地位的計數我得到follwing多維數組回mySQL count(*)數組的含義

Array 
(
    [0] => Array 
     (
      [status] => new 
      [0] => new 
      [count(*)] => 6 
      [1] => 6 
     ) 

    [1] => Array 
     (
      [status] => process 
      [0] => process 
      [count(*)] => 1 
      [1] => 1 
     ) 

    [2] => Array 
     (
      [status] => completed 
      [0] => completed 
      [count(*)] => 1 
      [1] => 1 
     ) 

) 

爲什麼有冗餘子陣列[status][0][count(*)][1]

+0

這不是一個SQL問題,它是如何將結果集返回給客戶端的...... – dnoeth

+0

使用'mysqli_fetch_assoc()'。可能會給陣列沒有冗餘列。 –

+0

@dnoeth - 你是SQL的廚師 - 謝謝 – Ben

回答

0

既然您已經選擇了'status'和'count(*)',這些鍵就存在於你的結果數組中。 結果數組的數字索引也有相同的結果。

-1

這是因爲你有三個狀態:新的,過程,完成。 你的SQL計算每個狀態包含多少條記錄。

[0]表示第一列。它等於[「狀態」]。
[1]是指第二欄。它等於[「count(*)」]。

0

希望這解決您的查詢。

Use mysqli_fetch_assoc($dbcon,$countQuery); 
//$dbcon = your db connection variable // $countQuery = your query 
0

這並不是說mysql擴展在PHP中被廢棄5.5+。

這是mysql_fetch_array

默認行爲,通過使用MYSQL_BOTH(默認),你會得到與兩個關聯和數字索引。使用MYSQL_ASSOC,您只能獲得關聯索引(如mysql_fetch_assoc()工作),使用MYSQL_NUM,您只能獲得數字索引(如mysql_fetch_row()工作)。

,僅保留關聯數組,爲mysql棄用擴展兼用:

mysql_fetch_array($queryResult, MYSQL_ASSOC) 
mysql_fetch_assoc($queryResult); 

mysqli

mysqli_fetch_array($queryResult, MYSQLI_ASSOC); 
mysqli_fetch_assoc($queryResult); 

// This will return all rows returned by query, while the other return only one row 
mysqli_fetch_all($queryResult, MYSQLI_ASSOC); 

* $queryResultmysql_query()或迴歸mysqli_query()