This article mainly introduces the output results of various parameters in PHPPDOfetch mode. the purpose of this article is to select different parameters based on actual needs, so that you do not need to test them one by one, it is too convenient for anyone to refer to the fetch mode function of PDO, but it is too troublesome to try to generate the desired results every time. the possible combinations are listed here.
The code is 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' => 'New this week ',
'Method' => 'movie ',
),
Array (
'Id' => '3 ',
'Name' => 'playing in replaying ',
'Method' => 'movie ',
),
)
*/
$ Data = $ dbAdapter-> query ("
SELECT name, method FROM category
")-> FetchAll (PDO: FETCH_COLUMN );
// Var_dump ($ data );
/*
Array (
'HBO ',
'New this week ',
'Hot ining ',
)
*/
$ 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' => 'New this week ',
'Method' => 'movie ',
),
'3' => array (
'Name' => 'playing in replaying ',
'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' => 'playing in replaying ',
),
)
*/
$ Data = $ dbAdapter-> query ("
SELECT id, name, method FROM category
")-> FetchAll (PDO: FETCH_UNIQUE | PDO: FETCH_COLUMN );
// Var_dump ($ data );
/*
Array (
'1' => 'HBO ',
'2' => 'New this week ',
'3' => 'hit in replaying ',
)
*/
$ Data = $ dbAdapter-> query ("
SELECT method, name, id FROM category
")-> FetchAll (PDO: FETCH_UNIQUE | PDO: FETCH_COLUMN );
// Var_dump ($ data );
/*
Array (
'Service' => 'HBO ',
'Movie '=> 'hot input ',
)
*/
$ 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' => 'New this week'
),
Array (
'Id' => '3'
'Name' => 'hit in replaying'
),
)
)
*/
$ 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 this week'
'Hot ining'
),
)
*/
$ 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 this week ';
Public $ method = 'movie ';
},
StdClass {
Public $ id = '3 ';
Public $ name = 'in replaying ';
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 this week ';
Public $ method = 'movie ';
},
Category_1 {
Public $ id = '3 ';
Public $ name = 'in replaying ';
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 this week ';
Public $ method = 'movie ';
},
Category_2 {
Public $ name = 'in replaying ';
Public $ method = 'movie ';
},
)
*/