我有一个正在构建的查询:
$filmQuery = "
SELECT nfc_film.title, nfc_film.film_id, nfc_film.description, nfc_film.release_year, nfc_film.rating, nfc_film.last_update, nfc_category.name FROM nfc_film
JOIN nfc_film_category
ON nfc_film.film_id = nfc_film_category.film_id
JOIN nfc_category
ON nfc_film_category.category_id = nfc_category.category_id";
if($search_term && $category){
$filmQuery .= "
WHERE
nfc_film.title LIKE :searchterm
AND
nfc_category.name = :category";
} else if ($search_term && !$category) {
$filmQuery .= "
WHERE
nfc_film.title LIKE :searchterm";
} else if (!$search_term && $category) {
$filmQuery .= "
WHERE
nfc_category.name = :category";
}
$filmQuery .= "
LIMIT 10
OFFSET 0
";
这似乎正常工作,我检查了变量并在我的DBMS中运行了它。
因此,我随后绑定了Params(包括用于另一个查询的param绑定):
if($search_term != ''){
$sqlGetFilms->bindParam(':searchterm', $sqlSearchTerm, PDO::PARAM_STR);
$sqlCountFilms->bindParam(':searchterm', $sqlSearchTerm, PDO::PARAM_STR);
}
if($category != ''){
$sqlGetFilms->bindParam(':category', $category, PDO::PARAM_STR);
$sqlCountFilms->bindParam(':category', $category, PDO::PARAM_STR);
}
if($page != ''){
$sqlGetFilms->bindParam(':page', $page, PDO::PARAM_INT);
}
执行此命令时得到的结果无济于事。
当我删除bindParams并将查询中的参数替换为静态值时,查询将返回结果。
那么,为什么bindParams破坏了我的查询?
编辑:
这些是1的SQL语句。否$search_term
或category
2. $search_term
和$category
:
SELECT nfc_film.title, nfc_film.film_id, nfc_film.description, nfc_film.release_year, nfc_film.rating, nfc_film.last_update, nfc_category.name
FROM nfc_film
JOIN nfc_film_category
ON nfc_film.film_id = nfc_film_category.film_id
JOIN nfc_category
ON nfc_film_category.category_id = nfc_category.category_id LIMIT 10 OFFSET 0
SELECT nfc_film.title, nfc_film.film_id, nfc_film.description, nfc_film.release_year, nfc_film.rating, nfc_film.last_update, nfc_category.name
FROM nfc_film
JOIN nfc_film_category
ON nfc_film.film_id = nfc_film_category.film_id
JOIN nfc_category
ON nfc_film_category.category_id = nfc_category.category_id
WHERE
nfc_film.title LIKE :searchterm
AND
nfc_category.name = :category LIMIT 10 OFFSET 0