我的数据库中有一个表,有这些字段:
-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);
返回一个数组,其中每个元素是查询的一行。
你需要在while循环中放入fetch关联来获取所有数据
While($data = $stmt->fetch(PDO::FETCH_ASSOC)){
//process data here
}