Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO Problem. Execute arguments not being read?

Tags:

php

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.

like image 761
Jamie Redmond Avatar asked May 28 '26 15:05

Jamie Redmond


1 Answers

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.

like image 150
Michael Clerx Avatar answered May 31 '26 05:05

Michael Clerx