PHP PDO fetch 模式各种参数的输出结果一览
发布:smiling 来源: PHP粉丝网 添加日期:2021-05-05 21:37:39 浏览: 评论:0
这篇文章主要介绍了PHP PDO fetch 模式各种参数的输出结果一览,本文目的是方便根据实际需求选择不同的参数,从而不用一个一个测试过去,需要的朋友可以参考下
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';
- },
- )
- */
Tags: PHP PDO fetch
相关文章
- ·PHP中通过Web 执行C/C++应用程序(2013-11-13)
- ·用PHP实现Ftp用户的在线管理(2013-11-13)
- ·用PHP自动把纯文本转换成Web页面(2013-11-13)
- ·用实例分析PHP5异常处理(2013-11-13)
- ·php5的simplexml解析错误(2013-11-13)
- ·PHP后门的隐藏技巧测试报告(2013-11-13)
- ·PHP缓存技术详谈(2013-11-27)
- ·利用PHP自定义错误处理器处理出错信息(2013-11-27)
- ·PHP作wap开发时遇到的问题(2013-11-27)
- ·php编写大型网站问题集(2013-11-27)
- ·php测试性能代码(2013-11-28)
- ·php 安全register globals设置为TRUE的危害(2013-11-28)
- ·XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数(2013-11-30)
- ·PHP中常用三种缓存技术(2013-11-30)
- ·新浪微博PHP版SDK的导致20007错误(2013-12-03)
- ·linux中phpMyAdmin错误提示Wrong permissions on configuration file, should no(2013-12-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)