Here is my function to return the most popular videos. For some reason it don't acknowledge the :limit. If I remove ':limit' and implicitly put in the number 10 it works.
Method:
function getPopularVideos($limit) {
$dbc = connectToDatabase();
$q = $dbc->prepare('SELECT * FROM video ORDER BY views DESC LIMIT 0, :limit');
$q->execute(array(':limit' => $limit));
return $q->fetchAll(PDO::FETCH_ASSOC);
}
Calling code:
$popularVideos = getPopularVideos(10);
Any idea's what I'm doing wrong. Little confused.
According to this comment on php.net this happens because the limit is being quoted, which wrecks the SQL syntax. The suggested workaround is using bindParam instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With