我正在根據類別爲網店製作一個過濾系統。這些類別與數據庫中的每一個內容一起保存。類別是整數。mysql正則表達式在準備語句中的問題
我能夠直接查詢數據庫,這給了我想要的結果。
mysql> select id, name, category, url, price from content where category REGEXP '1|2|3|4|5|6';
這將返回
+----+------------+----------+--------------------------+-------+
| id | name | category | url | price |
+----+------------+----------+--------------------------+-------+
| 1 | landschap1 | 2 | Ideal-landscape.jpg | 20 |
| 2 | landschap1 | 2 | landscape-Photograps.jpg | 20 |
| 4 | landschap1 | 2 | landscape.jpg | 25 |
| 5 | bunny | 4 | bunny.mov | 100 |
+----+------------+----------+--------------------------+-------+
當這種嘗試在PHP然而,我得到一個語法錯誤或訪問衝突。由於代碼使用的數據庫用戶沒有缺少權限,因此必須是語法錯誤。
查詢我試圖執行:
if(empty($_SESSION['filter'])){
$halfFilt = array(1,2,3,4,5,6);
$filter = implode('|', $halfFilt);
//If filter halfFilt is empty, assume no filter is selected.
$sql = "SELECT * FROM content WHERE id=:id AND WHERE category REGEXP :filter";
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->bindParam(':filter', $filter);
$sth->execute();
$results = $sth->fetch(PDO::FETCH_ASSOC);
return $results;
當我嘗試執行此查詢這是它引發錯誤:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE category REGEXP '1|2|3|4|5|6'' at line 1' in /var/www/portfolio/photoshop/include/database.php:89