2
我試圖限制從SQL查詢返回的結果數量,但由於某種原因它返回NULL。如果我刪除了限制,一切正常。我已經在Sequel Pro中測試了這個查詢,並且它在LIMIT中也能正常工作。我在這裏做錯了什麼?PDO Prepare Statement Limit Not Working
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;
}
}
什麼版本的PHP? – j08691
我正在使用5.4.10 –
另請參閱[PDO準備語句在LIMIT語句中導致錯誤](http://stackoverflow.com/a/15991623/689579) – Sean