时间: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邮箱联系删除。
本文实例讲述了php使用PDO获取结果集的方法。分享给大家供大家参考,具体如下:fetch()方法fetch()方法用于获取结果集的下一行,语法如下:mixed
linux和windows的区别linux优缺点一览linux和windows的区别linux优缺点一览linux和windows的区别linux优缺点一览li
linux和windows的区别linux优缺点一览linux和windows的区别linux优缺点一览linux和windows的区别linux优缺点一览li
本文实例讲述了PHP使用PDO抽象层获取查询结果的方法。分享给大家供大家参考,具体如下:PHP使用PDO抽象层获取查询结果,主要有三种方式:(1)PDO::qu
天猫商城今年度各种目服务费信用卡年费一览表艰辛您查询,在《天猫2020年度各类目年费软件服务费一览表》网页页面中检索您要想查询的品类,可以协助您迅速找到答案哦。