Is there a way to fetch associative array grouped by the values of a specified column with PDO?

前端 未结 8 1464
轮回少年
轮回少年 2020-11-28 07:39

For example, let\'s use some simple data set

+---------+------+------+------------+
| name    | age  | sex  | position   |
+---------+------+------+---------         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 08:10

    Not sure why no one has posted the following solution, but it works perfectly for me:

    PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC
    

    So, changing your statement to:

    $pdo->query('SELECT * FROM employee')->fetchAll(PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);
    

    should be exactly what you want.

提交回复
热议问题