The PDO Fetch mode feature is so handy, but it's too much trouble to try to produce the desired results, and here's a list of possible combinations.
Copy Code code as follows:
<?php
$dbAdapter = new PDO ("Mysql:host=localhost;dbname=test", "root", "1234");
$dbAdapter->exec ("SET NAMES ' UTF8 ';");
$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (PDO::FETCH_ASSOC);
Var_dump ($data);
/*
Array
Array
' id ' => ' 1 ',
' Name ' => ' HBO ',
' Method ' => ' service ',
),
Array
' ID ' => ' 2 ',
' Name ' => ' this week's new film ',
' Method ' => ' movie ',
),
Array
' ID ' => ' 3 ',
' Name ' => ' in the Heat ',
' Method ' => ' movie ',
),
)
*/
$data = $dbAdapter->query ("
SELECT name, method from category
")->fetchall (Pdo::fetch_column);
Var_dump ($data);
/*
Array
' HBO ',
' This week's new film ',
' Hot in ',
)
*/
$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (Pdo::fetch_unique | PDO::FETCH_ASSOC);
Var_dump ($data);
/*
Array
' 1 ' => array (
' Name ' => ' HBO ',
' Method ' => ' service ',
),
' 2 ' => array (
' Name ' => ' this week's new film ',
' Method ' => ' movie ',
),
' 3 ' => array (
' Name ' => ' in the Heat ',
' Method ' => ' movie ',
),
)
*/
$data = $dbAdapter->query ("
SELECT method, ID, name from category
")->fetchall (Pdo::fetch_unique | PDO::FETCH_ASSOC);
Var_dump ($data);
/*
Array
' Service ' => array (
' id ' => ' 1 ',
' Name ' => ' HBO ',
),
' Movie ' => Array (
' ID ' => ' 3 ',
' Name ' => ' in the Heat ',
),
)
*/
$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (Pdo::fetch_unique | Pdo::fetch_column);
Var_dump ($data);
/*
Array
' 1 ' => ' HBO ',
' 2 ' => ' This week's new film ',
' 3 ' => ' in ' Heat ',
)
*/
$data = $dbAdapter->query ("
SELECT method, name, ID from category
")->fetchall (Pdo::fetch_unique | Pdo::fetch_column);
Var_dump ($data);
/*
Array
' Service ' => ' HBO ',
' Movie ' => ' Hot in ',
)
*/
$data = $dbAdapter->query ("
SELECT method, ID, name from category
")->fetchall (Pdo::fetch_assoc | Pdo::fetch_group);
Var_dump ($data);
/*
Array
' Service ' => array (
Array
' id ' => ' 1 '
' Name ' => ' HBO '
),
)
' Movie ' => Array (
Array
' ID ' => ' 2 '
' Name ' => ' this week's new film '
),
Array
' ID ' => ' 3 '
' Name ' => ' hot-map '
),
)
)
*/
$data = $dbAdapter->query ("
SELECT method, name, ID from category
")->fetchall (Pdo::fetch_group | Pdo::fetch_column);
Var_dump ($data);
/*
Array
' Service ' => array (
' HBO '
),
' Movie ' => Array (
' New movie of the Week '
' Hot in '
),
)
*/
$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (Pdo::fetch_obj);
Var_dump ($data);
/*
Array
stdclass{
Public $id = ' 1 ';
Public $name = ' HBO ';
Public $method = ' service ';
},
stdclass{
Public $id = ' 2 ';
Public $name = ' new film of the Week ';
Public $method = ' movie ';
},
stdclass{
Public $id = ' 3 ';
Public $name = ' hot map ';
Public $method = ' movie ';
},
)
*/
Class Category_1 {}
$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (Pdo::fetch_class | Pdo::fetch_props_late, "category_1");
Var_dump ($data);
/*
Array
category_1{
Public $id = ' 1 ';
Public $name = ' HBO ';
Public $method = ' service ';
},
category_1{
Public $id = ' 2 ';
Public $name = ' new film of the Week ';
Public $method = ' movie ';
},
category_1{
Public $id = ' 3 ';
Public $name = ' hot map ';
Public $method = ' movie ';
},
),
*/
Class Category_2 {
Public $name;
Public $method;
Public Function __construct () {}
Public Function __set ($name, $value) {}
}
$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (Pdo::fetch_class | Pdo::fetch_props_late, "category_2");
Var_dump ($data);
/*
Array
category_2{
Public $name = ' HBO ';
Public $method = ' service ';
},
category_2{
Public $name = ' new film of the Week ';
Public $method = ' movie ';
},
category_2{
Public $name = ' hot map ';
Public $method = ' movie ';
},
)
*/