PDO::FETCH_ASSOC値を配列で取得

PHP::PEAR

PECL/pdo/fetch/FETCH_ASSOCは結果を配列で返す。
カラム名と値のハッシュ値として値を返す

利用できるメソッド

PDOクラス
query
PDOStatementクラス
fetch,fetchAll,fetchColumn

組み合わせ可能なFETCHパラメータ

PDO::FETCH_GROUP

fetchメソッドサンプルコード

$sql = <<<SQL
       SELECT * FROM user_table 

SQL;

$stmtObj = $dbObj->query( $sql );
while ( $rowObjs = $stmtObj->fetch( PDO::FETCH_ASSOC ) )
 print_r( $rowObjs );

結果

Array                  
(                      
   [users_id] => 1    
   [users_name] => TARO
   [users_email] => taro@matrix.jp
)                      
Array                  
(                      
   [users_id] => 2    
   [users_name] => JIRO
   [users_email] => jiro@matrix.jp
)                      

fetchAllメソッドサンプルコード

while ( $rowObjs = $stmtObj->fetchAll( PDO::FETCH_ASSOC ) )
 print_r( $rowObjs );

結果

Array                  
(                      
   [0] => Array       
       (              
           [users_id] => 1
           [users_name] => TARO
           [users_email] => taro@matrix.jp
       )              
                      
   [1] => Array       
       (              
           [users_id] => 2
           [users_name] => JIRO
           [users_email] => jiro@matrix.jp
       )              

組み合わせ使用サンプルコード

$sql = <<<SQL
       SELECT * FROM user_table LEFT JOIN user_homepage USING( users_id );
SQL;
$stmtObj = $dbObj->query( $sql );
while ( $rowObjs = $stmtObj->fetch(PDO::FETCH_LAZY | PDO::FETCH_OBJ) )
 print_r( $rowObjs );

結果

Array                  
(                      
   [1] => Array       
       (              
           [0] => Array
               (      
                   [users_name] => TARO
                   [users_email] => taro@matrix.jp
                   [homepage_id] => 1
                   [homepage_url] => TARO Homepage
                   [homepage_name] => http://taro.matrix.jp
               )      
                      
       )              
                      
   [2] => Array       
       (              
           [0] => Array
               (      
                   [users_name] => JIRO
                   [users_email] => jiro@matrix.jp
                   [homepage_id] => 1
                   [homepage_url] => JIRO Homepage
                   [homepage_name] => http://jiro.matrix.jp
               )      
                      
       )              
                      
・・・・・
・・・・・
                      
   [4] => Array       
       (              
           [0] => Array
               (      
                   [users_name] => dozo
                   [users_email] => dozo@matrix.jp
                   [homepage_id] => 1
                   [homepage_url] => dozo Homepage
                   [homepage_name] => http://dozo.matrix.jp
               )      
                      
           [1] => Array
               (   
                   [users_name] => dozo
                   [users_email] => dozo@matrix.jp
                   [homepage_id] => 2
                   [homepage_url] => dozo Homepage2
                   [homepage_name] => http://dozo.matrix.jp/pear/
               )
           [2] => Array
               (   
                   [users_name] => dozo
                   [users_email] => dozo@matrix.jp
                   [homepage_id] => 3
                   [homepage_url] => dozo Homepage3
                   [homepage_name] => http://dozo.matrix.jp/blog/
               )
       
       )

)

お名前:


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Third Project
証券会社ランキング比較