2013-10-08 27 views
0

我有這個,一切都工作得很好(我有一個通用的表器,但現在我有從流浪):裝滿mysqli_fetch_assoc從查詢陣列()

while ($x = mysqli_fetch_assoc($result)) 
{ 
    $fields[] = $x['Field']; 
} 

現在我有類似的東西這樣的:

$result = mysqli_query($con, 'SELECT r.id AS ID, CONCAT(g.fname, g.lname) AS Name, r.apple AS Apple, 
          r.dog AS Dog, DATEDIFF(r.Dog, r.Apple) AS Days, 
          r.total_price AS "Total Price", u.name AS Name, r.in AS "In", 
          r.out AS "Out", r.time_in AS "Time In", r.time_out AS "Time Out", 
          CONCAT(c.fname,c.lname) AS Charlie, r.here AS "Apple", 
          r.leave AS "Dog" 
          FROM really r, georgia g, unit u, charlie c 
          WHERE g.id = r.georgia AND r.unit = u.id AND r.charlie = c.id 
          HAVING r.in = TRUE AND r.out = FALSE'); 

    //fill fields array with fields from table in database 
    while ($x = mysqli_fetch_assoc($result)) 
    { 
     $fields[] = $x['Field']; 
    } 

我現在得到一個錯誤的行$fields[] = $x['Field'];因爲這個詞Field的。爲什麼?因爲我現在有一個完整的查詢?我怎樣才能解決這個問題,而不參考每個字段名稱?

+1

爲什麼不使用'mysqli_fetch_array'? – Jacques

回答

1

因爲沒有一個名爲Field在您查詢結果字段:

'SELECT r.id AS ID, CONCAT(g.fname, g.lname) AS Name, r.apple AS Apple, 
         r.dog AS Dog, DATEDIFF(r.Dog, r.Apple) AS Days, 
         r.total_price AS "Total Price", u.name AS Name, r.in AS "In", 
         r.out AS "Out", r.time_in AS "Time In", r.time_out AS "Time Out", 
         CONCAT(c.fname,c.lname) AS Charlie, r.here AS "Apple", 
         r.leave AS "Dog" 
         FROM really r, georgia g, unit u, charlie c 
         WHERE g.id = r.georgia AND r.unit = u.id AND r.charlie = c.id 
         HAVING r.in = TRUE AND r.out = FALSE' 

,我們在您查詢結果的某些字段:IDNameApple,等等。你可以嘗試獲取這些領域如下所示,或更改您的查詢命令。

while ($x = mysqli_fetch_assoc($result)) 
{ 
    $fields[] = $x['ID']; 
}