2015-08-16 60 views
0

我有一個表在我的數據庫具有這些字段:查詢mysql數據庫,並返回所有出現

-id 
-video_title 
-video_article 
-video_category 

現在,我想用video_category進行查詢,到目前爲止我已經試過這個代碼:

public function related_videos($current_video_category) 
{ 
    $sql = "SELECT * FROM English WHERE video_category = :current_video_category"; 
    $stmt = $this->pdo->prepare($sql); 
    $result = $stmt->execute(array(":current_video_category" => $current_video_category)); 
    $data = $stmt->fetch(PDO::FETCH_ASSOC); 

    return $data; 
} 

的問題是它只返回第一次出現,我不知道如何在一次返回$current_video_category所有出現。

任何幫助將非常感激。

回答

2

你只取一行:如果你想獲取從語句中的所有可用行

$data = $stmt->fetch(PDO::FETCH_ASSOC); 

,使用fetchAll

$data = $stmt->fetchAll(PDO::FETCH_ASSOC); 

這將返回一個數組,其中每個元素都是一個排從查詢。

+0

使用fetchall就像一個魅力對我來說!謝謝@MatsLindh –

0

你需要把while循環獲取assoc命令來獲取所有數據

While($data = $stmt->fetch(PDO::FETCH_ASSOC)){ 

//process data here 

}