2015-04-26 76 views
1

我通常感到困惑的適當方式,一旦你執行從查詢中獲取數據。我的最終目標是能夠顯示所有記錄的列表,但我不知道如何去做。我已經嘗試了幾種不同的東西,但沒有一個給我相當我正在尋找的東西。例如,當我搜索蝙蝠俠(這應該有[蝙蝠俠,500,1993]記錄,併爲[蝙蝠俠,499,1993]記錄)這裏就是我已經試過:如何正確獲取數據從查詢生成陣列

$search->execute(); 

$result = $search->fetch(PDO::FETCH_ASSOC); 
print_r($result); 

哪給我:Array([comicTitle] =>蝙蝠俠[comicIssue] => 500 [releaseDate] => 1993)

這或多或少是我所尋找的格式,只不過它只有一條記錄而不是全部。

$search->execute(); 

$result = $search->fetchAll(PDO::FETCH_ASSOC); 
print_r($result); 

這使我:陣列([0] =>數組([comicTitle] =>蝙蝠俠[comicIssue] => 500 [RELEASEDATE] => 1993)[1] =>數組([comicTitle] = >蝙蝠俠[comicIssue] => 499 [RELEASEDATE] => 1993))

$search->execute(); 

$result = $search->fetch(PDO::FETCH_ASSOC); 
foreach($result as $row) { 
     echo $row['comicTitle']."<BR>"; 
     echo $row['comicIssue']."<BR>"; 
     echo $row['releaseDate']."<BR>"; 

此回聲3 B的,3 5的,和3 1與一個非法串偏移每個之間警告。

所以,如果我想呼應線讀取

"Title: " . $whatevertitle . " Issue: " . $whateverissue . " Release: " $whateverelease 

找到的每個記錄,我怎麼去真正從返回數組獲得這些價值?

回答

0

使用->fetch(PDO::FETCH_ASSOC)

$search->execute(); 

while($row = $search->fetch(PDO::FETCH_ASSOC)){ 
     echo "Title: " . $row['comicTitle']."<BR>"; 
     echo "Issue: " . $row['comicIssue']."<BR>"; 
     echo "Release: " . $row['releaseDate']."<BR>"; 
} 

使用->fetchAll(PDO::FETCH_ASSOC)

$search->execute(); 

$result = $search->fetchAll(PDO::FETCH_ASSOC); 
foreach($result as $row) { 
     echo "Title: " . $row['comicTitle']."<BR>"; 
     echo "Issue: " . $row['comicIssue']."<BR>"; 
     echo "Release: " . $row['releaseDate']."<BR>"; 
}