从MySQL PDO查询结果创建数组

时间:2022-10-13 12:35:56

I have the table "users" :

我有“用户”表:

________________________________________________
|---ID---|---Email---|---Birthday---|---City---|
|---1----|asd@asd.as-|-02/03/1989---|---NY-----|
|---2----|asq@avt.al-|-06/01/1990---|---LA-----|
------------------------------------------------


This is the query i use in pdo :

这是我在pdo中使用的查询:

$qry = "SELECT * FROM users";

What i want is to stock the result from mysql pdo query in an array variable like this:

我想要的是将mysql pdo查询的结果存储在数组变量中,如下所示:

$result = array(
   array("id"=>1,
    "email"=>"asd@asd.as".
    "birthday"=>"02/03/1989",
    "city"=>"NY"),
   array("id"=>2,
    "email"=>"asq@avt.al".
    "birthday"=>"06/01/1990",
    "city"=>"LA")
);

How can i insert all the results in an array like this in php ?

我如何在php中插入这样的数组中的所有结果?

1 个解决方案

#1


1  

You can use fetchAll():

你可以使用fetchAll():

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
          ^^^^^ or whatever you use

#1


1  

You can use fetchAll():

你可以使用fetchAll():

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
          ^^^^^ or whatever you use