I am trying to bind values to a query in PHP. I have done this successfully many times, but for some reason my code isn't working.
function get_movies($vars, $page) {
global $db;
$get_movies = $db->prepare('SELECT * FROM `movies` WHERE LOWER(genres) LIKE :genre AND `qualities` LIKE :quality AND `rating` >= :imdb_min AND `rating` <= :imdb_max AND `year` >= :year_min AND `year` <= :year_max ORDER BY id DESC');
$get_movies->bindValue(':genre', $vars['genre']);
$get_movies->bindValue(':quality', $vars['quality']);
$get_movies->bindValue(':imdb_min', $vars['imdb_min']);
$get_movies->bindValue(':imdb_max', $vars['imdb_max']);
$get_movies->bindValue(':year_min', $vars['year_min']);
$get_movies->bindValue(':year_max', $vars['year_max']);
try {
$get_movies->execute();
$movies = $get_movies->fetchAll(); // list of all movies fitting parameters
$movie_offset = ($page - 1) * VIDEOS_PER_PAGE;
$movies = array_slice($movies, $movie_offset, VIDEOS_PER_PAGE);
return $movies;
} catch (Exception $e) {
throw $e;
return false;
}
}
The above code does not work. No exception is thrown, but it returns 0 results. However, if I built the query manually (ex: replacing each :key with the $vars['key'] and preparing the statement from the resulting string) the query returns results perfectly fine.
Any tips would be greatly appreciated.
Edit: Here's the $vars array passed to the $get_movies function.
$vars = array(
'genre' => "Action",
'quality' => 1080,
'imdb_min' => 0.1,
'imdb_max' => 10.0,
'year_min' => 2000,
'year_max' => 2019
);