PHP PDO fetch 模式各種參數的輸出結果一覽,pdofetch_PHP教程

來源:互聯網
上載者:User

PHP PDO fetch 模式各種參數的輸出結果一覽,pdofetch


PDO 的 fetch 模式功能實在是太方便了,但每次要產生想要的結果都要試太麻煩了,這裡列出可能的組合。
複製代碼 代碼如下:
<?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' => '本周新片',
'method' => 'movie',
),
array(
'id' => '3',
'name' => '熱映中',
'method' => 'movie',
),
)
*/



$data = $dbAdapter->query("
SELECT name, method FROM category
")->fetchAll(PDO::FETCH_COLUMN);

//var_dump($data);
/*
array(
'HBO',
'本周新片',
'熱映中',
)
*/



$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' => '本周新片',
'method' => 'movie',
),
'3' => array(
'name' => '熱映中',
'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' => '熱映中',
),
)
*/



$data = $dbAdapter->query("
SELECT id, name, method FROM category
")->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_COLUMN);

//var_dump($data);
/*
array(
'1' => 'HBO',
'2' => '本周新片',
'3' => '熱映中',
)
*/



$data = $dbAdapter->query("
SELECT method, name, id FROM category
")->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_COLUMN);

//var_dump($data);
/*
array(
'service' => 'HBO',
'movie' => '熱映中',
)
*/




$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' => '本周新片'
),
array(
'id' => '3'
'name' => '熱映中'
),
)
)
*/




$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(
'本周新片'
'熱映中'
),
)
*/





$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 = '本周新片';
public $method = 'movie';
},
stdClass{
public $id = '3';
public $name = '熱映中';
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 = '本周新片';
public $method = 'movie';
},
Category_1{
public $id = '3';
public $name = '熱映中';
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 = '本周新片';
public $method = 'movie';
},
Category_2{
public $name = '熱映中';
public $method = 'movie';
},
)
*/

http://www.bkjia.com/PHPjc/939417.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/939417.htmlTechArticlePHP PDO fetch 模式各種參數的輸出結果一覽,pdofetch PDO 的 fetch 模式功能實在是太方便了,但每次要產生想要的結果都要試太麻煩了,這裡列出...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.