Output results of various parameters in PHPPDOfetch mode

Source: Internet
Author: User
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 ';
},
)
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.