PHP PDO Fetch mode output of various parameters list _php tips

Source: Internet
Author: User

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 ';
},
)
*/

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.