2012-06-16 177 views
4
<?php 
$sth = $dbh->prepare("SELECT name, colour FROM fruit"); 
$sth->execute(); 

/* Fetch all of the remaining rows in the result set */ 
print("Fetch all of the remaining rows in the result set:\n"); 
$result = $sth->fetchAll(); 
print_r($result); 
?> 

上面的例子将输出类似于:PDO :: FETCH_ASSOC什么是PDO :: FETCH_ARRAY?

抓取所有剩余行的结果集:

Array 
(
    [0] => Array 
     (
      [NAME] => pear 
      [0] => pear 
      [COLOUR] => green 
      [1] => green 
     ) 

    [1] => Array 
     (
      [NAME] => watermelon 
      [0] => watermelon 
      [COLOUR] => pink 
      [1] => pink 
     ) 

) 

是对任何选项来获得像my_sql_fetch_array和结果的结果应该是:

Array 
(
    [0] => Array 
     (

      [0] => pear 
      [1] => green 
     ) 

    [1] => Array 
     (

      [0] => watermelon    
      [1] => pink 
     ) 

) 

回答

8

http://php.net/manual/en/pdostatement.fetch.php

$result = $sth->fetchAll(PDO::FETCH_NUM); 
+2

从http://php.net/manual/en/pdostatement.fetchall.php我没有看到关于选项FETCH_NUM的说。任何地方都适合你的小费。 谢谢 – sophie

+0

仅供参考,FETCH_NUM在fetch()not fetchAll()文档中列出:http://www.php.net/manual/en/pdostatement.fetch.php – Rafa

+0

@Rafa Hmm ...已更新 – Musa

0

尝试使用这种

$data = $sth->fetch(); 
print_r($data); 

希望它会帮助你。