使用mysqli_fetch_assoc()从查询中填充数组

时间:2013-10-08 02:31:05

标签: php html mysql sql

我有这个并且一切正常(我有一个通用的表构建器,但现在我不得不偏离它):

 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 个答案:

答案 0 :(得分: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'];
}