PDO problem. Perform arguments that are not readable?

Here is my function to bring back the most popular videos. For some reason it does not recognize: limit. If I remove the ': limit' and implicitly put it 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);
}

Call Code:

$popularVideos = getPopularVideos(10);

Any idea what I'm doing wrong. A bit confused.

+3
source share
1 answer

According to this comment on php.net this happens because the limit is quoted, which breaks the SQL syntax. The proposed workaround uses bindParam instead.

+1
source

Source: https://habr.com/ru/post/1763310/


All Articles