- <?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';
- },
- )
- */
2013-04-04 22:05
PDO fetch 模式的回傳結果
PDO 的 fetch 模式功能實在是太方便了,但每次要產生想要的結果都要試太麻煩了,這裡列出可能的組合。
相關文章 :
- [Python] Flask Log 配置
- [Python] Flask 自訂日期的 Json 轉換
- [Python] Flask MySQL 連線管理
- [Python] Flask 錯誤處裡
- 架構解釋
- PDO fetch 模式的回傳結果
- [Ubuntu] 安裝 Oracle Client 與 PDO_OCI
- [Oracle] PDO-OCI 的 DSN 連接字串
- [PHP] 繼承改寫 PDO
- [PHP] PDO 資料庫操作方法(快速範例)
- 全域的 Exception 處理
- [PHP] 從 RGB 轉換到 HSB
- [PHP] 從 Hex 轉換到 RGB
- [PHP][Java][C#] 用 XSD 驗證 XML
- [轉載] PHP 配置open_basedir,让各虚拟站点独立运行
訂閱:
張貼留言 (Atom)
0 回應:
張貼留言