簡體   English   中英

通過字段查詢mysql數據庫並返回所有出現的事件

[英]query mysql database by field and return all occurrences

我的數據庫中有一個表,其中包含以下字段:

-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匹配項。

任何幫助將非常感激。

您只獲取一行:

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

如果要從語句中獲取所有可用的行,請使用fetchAll

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

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

您需要將獲取的assoc放入while循環中以獲取所有數據

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

//process data here

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM