所以我正在構建一個搜索腳本並且要傳遞兩個變量,但首先我要確保SQL QUERY是正確的,因此我現在對這個變量進行了硬編碼。所以我的變量是此SQL IF語句有什麼問題?
$comma_separated = "'Alberta','Ontario'";
是能否通過對查詢,它看起來像這樣過去了:
$sql = "SELECT * FROM persons WHERE 1=1";
if ($firstname)
$sql .= " AND firstname='" . mysqli_real_escape_string($mysqli,$firstname) . "'";
if ($surname)
$sql .= " AND surname='" . mysqli_real_escape_string($mysqli,$surname) . "'";
if ($province)
$sql .= " AND province='" . mysqli_real_escape_string($mysqli,$comma_separated) . "' WHERE province IN ($comma_separated)";
$sql .= " ORDER BY surname";
,然後運行查詢時,我得到這個消息:
cannot run the query because: 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 province IN ('Alberta','Ontario') ORDER BY surname LIMIT 0, 5' at line 1
但對我來說,查詢看起來是正確的,我在這裏錯過了什麼?
在此先感謝。
發佈完整的查詢 –
編輯。請參閱上文。 – JLA
您在查詢中使用過'WHERE' –