PDO 的 fetch 模式功能实在是太方便了,但每次要产生想要的结果都要试太麻烦了,这里列出可能的组合。
复制代码 代码如下:
<"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';
},
)
*/
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 英雄联盟第二届老头杯什么时候开始 老头杯s2赛程时间队伍名单汇总
- AI赋能卓越显示技术共筑数字未来:三星显示器产品矩阵亮相2024进博会
- 技术剖析:天玑9400如何打造移动最强GPU和游戏体验?
- 顶级装备 实力登顶:三星显示器双十一焕新升级最后冲刺
- 陈影《绝色靓声》WAV+CUE
- 龚玥《禅是一枝花(6N纯银SQCD)》原抓WAV+CUE
- 刘德丽《寂寞在唱歌HQCD+A2HD5》[WAV+CUE]
- 萧亚轩《钻石糖》金牌大风[WAV+CUE][989M]
- 王菲《王菲精选-菲卖品》环球唱片SHM-SACD[ISO][1.9G]
- 孙露《一抹伤HQ》头版限量[WAV+CUE][1G]
- 黄安.1989-一切从头(TP版)【天际唱片】【FLAC分轨】
- 群星.1994-浓情蜜意情歌精丫华纳】【WAV+CUE】
- 邓丽君.1983-淡淡幽情(2022环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 试音天碟《专业测试第一天碟》经典天碟精选[WAV分轨][1G]
- 试音典范 《情惹发烧情HQCD》人声发烧极品 [WAV+CUE][1G]