PHP PDO fetch 模式各种参数的输出结果一览
程序员文章站
2022-05-18 22:01:29
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';
},
)
*/
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';
},
)
*/
上一篇: linux中cd命令使用详解