我正在尝试运行以下查询: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_filmJOIN nfc_film_category ON nfc_film.film_id = nfc_film_category.film_idJOIN nfc_category ON nfc_film_category.category_id = nfc_category.category_id LIMIT 10 OFFSET 0如果我将它放入“用于 SQLite 的数据库浏览器”中,这将起作用,返回正确的字段。但是当试图在 PHP 中实现它时它不起作用:$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_filmJOIN nfc_film_category ON nfc_film.film_id = nfc_film_category.film_idJOIN 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 :page";如果两者都没有category或searchterm没有找到,那么这将解析为: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 :page然后我准备、绑定和执行查询:$sqlGetFilms = $dbConn->prepare($filmQuery);$sqlGetFilms->bindParam(':searchterm', $sqlSearchTerm);$sqlGetFilms->bindParam(':category', $category);$sqlGetFilms->bindParam(':page', $page);$sqlGetFilms->execute();$query = $sqlGetFilms->fetchAll();当诺斯searchterm和category被发现,返回的记录。但是此代码中出现的任何其他情况都不会返回任何记录,即使它们在“DB Browser for SQLite”中运行时出现
1 回答
呼如林
TA贡献1798条经验 获得超3个赞
您可能会发现,当您只使用一个或其他术语时,您会收到一个错误,类似于尝试绑定到一个不存在的变量。
这(我认为)下降到了,你有条件地生成SQL,你可能有:searchterm或:category或两者-但你总是试图并绑定到他们两个。
所以相反,如果有一个值要绑定 - 然后单独绑定每个术语......
if(!empty($search_term)){
$sqlGetFilms->bindParam(':searchterm', $sqlSearchTerm);
}
if(!empty($category)) {
$sqlGetFilms->bindParam(':category', $category);
}
- 1 回答
- 0 关注
- 187 浏览
添加回答
举报
0/150
提交
取消