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

时间:2021-05-26

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);




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

//var_dump($data);




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

//var_dump($data);




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

//var_dump($data);




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

//var_dump($data);




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

//var_dump($data);





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

//var_dump($data);





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

//var_dump($data);






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

//var_dump($data);








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);






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);

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章