php的pdo的fetch大于0,PHPPDOfetch模式各种参数的输出结果一览,pdofetch_PHP教程

PHP PDO fetch 模式各种参数的输出结果一览,pdofetch

PDO 的 fetch 模式功能实在是太方便了,但每次要产生想要的结果都要试太麻烦了,这里列出可能的组合。

代码如下:

$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 模式功能实在是太方便了,但每次要产生想要的结果都要试太麻烦了,这里列出...

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值