我试图限制从 SQL 查询返回的结果数量,但由于某种原因它返回 NULL。如果我删除 LIMIT,一切正常。我已经在 Sequel Pro 中测试了该查询,它也适用于 LIMIT。我在这里做错了吗?
public static function getMostViewedPictures($limit = 5) {
$dbh = self::connectToDatabase();
$sql = "SELECT
picture.`title`,
picture.`description`,
picture.`slug`,
picture.`image`,
picture.`timestamp`,
picture.`views`,
category.category as category
FROM picture
LEFT JOIN category
ON picture.category_id = category.id
ORDER BY picture.views ASC
LIMIT 0, :limit";
$sth = $dbh->prepare($sql);
$sth->execute(array(':limit' => $limit));
if($results = $sth->fetchAll(PDO::FETCH_OBJ)) {
$pictures = array();
foreach($results as $result) {
$pictures[] = new Picture(
$result->title,
$result->description,
$result->slug,
$result->timestamp,
$result->category,
$result->views,
$result->image
);
}
return $pictures;
} else {
return null;
}
}