2012-06-16 116 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); 

希望它會幫助你。