PHP PDO Fetch mode output of various parameters at a glance, pdofetch_php tutorial

Source: Internet
Author: User

PHP PDO Fetch mode output list of various parameters, Pdofetch


The Fetch mode feature of PDO is very convenient, but it is too cumbersome to produce the desired results each time, here is a list of possible combinations.
Copy CodeThe 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 film of the Week ',
' Method ' = ' movie ',
),
Array
' id ' = ' 3 ',
' Name ' = ' Hot in the mirror ',
' Method ' = ' movie ',
),
)
*/



$data = $dbAdapter->query ("
SELECT name, method from category
")->fetchall (Pdo::fetch_column);

Var_dump ($data);
/*
Array
' HBO ',
' New film of the Week ',
' In the heat of the image ',
)
*/



$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 film of the Week ',
' Method ' = ' movie ',
),
' 3 ' = = Array (
' Name ' = ' Hot in the mirror ',
' 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 ' = ' Hot in the mirror ',
),
)
*/



$data = $dbAdapter->query ("
SELECT ID, name, method from category
")->fetchall (Pdo::fetch_unique | Pdo::fetch_column);

Var_dump ($data);
/*
Array
' 1 ' = ' HBO ',
' 2 ' = ' new film of the Week ',
' 3 ' = ' hot in ',
)
*/



$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 ' = ' new film of the Week '
),
Array
' id ' = ' 3 '
' Name ' = ' Hot in the mirror '
),
)
)
*/




$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 film of the Week '
' Hot reflection '
),
)
*/





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

http://www.bkjia.com/PHPjc/939417.html www.bkjia.com true http://www.bkjia.com/PHPjc/939417.html techarticle PHP PDO fetch mode The output of various parameters at a glance, Pdofetch PDO's fetch mode function is too convenient, but every time to produce the desired results are to try too much trouble, listed here ...

  • 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.