[英]MySQL syntax error on using if condition in WHERE clause
I am getting syntax error when I am using if
conditions in the WHERE
clause of my Mysql query. 当我在Mysql查询的
WHERE
子句中使用if
条件时,出现语法错误。 For example, if I put in WHERE 1
with only the condition like sector = 2
, it is working. 例如,如果仅在条件
sector = 2
的情况下放入WHERE 1
,则它可以正常工作。 But when I put the if
conditions, it is not working anymore. 但是,当我放入
if
条件时,它不再起作用。
$query= "SELECT
P.id
,P.price
,P.contract
,P.property_type
,P.sector
,P.title
,P.address
,P.bedrooms
,P.bathrooms
,P.price
,P.m2
,P.text_english
,P.photo_01
,P.utilities
,P.google_maps
,P.date
,CT.id
,CT.english_text
,PT.id
,PT.english
,C.cityname
,S.sectorname
,S.id
,O.ownername
,O.phone_one
,O.phone_two
,O.email
,O.notes
FROM properties P
JOIN contract CT
ON CT.id = P.contract
JOIN property_type PT
ON PT.id = P.property_type
JOIN city C
ON C.id = P.city
JOIN sector S
ON S.id = P.sector
JOIN owner O
ON O.id = P.owner WHERE 1";
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
if (!empty($property_type)) { $query .= " AND P.property_type = '$property_type'"; }
if (!empty($contract)) { $query .= " AND P.contract = '$contract'"; }
if (!empty($minimum_price)) { $query .= " AND P.price BETWEEN '$minimum_price' AND '$maximum_price'"; }
if (!empty($m2_from)) { $query .= " AND P.m2 BETWEEN '$m2_from' AND '$m2_until'"; }
if (!empty($bedrooms)) { $query .= " AND P.bedrooms = '$bedrooms'"; }
This is the error: 这是错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
致命错误:消息为“ SQLSTATE [42000]”的未捕获的异常“ PDOException”:语法错误或访问冲突:1064 SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'P.sector = 2 LIMIT 0, 30' at line 43' in E:\\xampp\\htdocs\\dolche\\admin\\class\\pagination.php:451 Stack trace: #0 E:\\xampp\\htdocs\\dolche\\admin\\class\\pagination.php(451): PDOStatement->execute() #1 E:\\xampp\\htdocs\\dolche\\admin\\search.php(190): pagination->execute() #2 {main} thrown in E:\\xampp\\htdocs\\dolche\\admin\\class\\pagination.php on line 451
检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在E:\\ xampp \\ htdocs \\ dolche \\ admin \\ class \\ pagination.php中的第43行的'P.sector = 2 LIMIT 0,30'附近使用: 451堆栈跟踪:#0 E:\\ xampp \\ htdocs \\ dolche \\ admin \\ class \\ pagination.php(451):PDOStatement-> execute()#1 E:\\ xampp \\ htdocs \\ dolche \\ admin \\ search.php(190 ):pagination-> execute()#2 {main}在第451行的E:\\ xampp \\ htdocs \\ dolche \\ admin \\ class \\ pagination.php中抛出
Any help resolving this issue will be very welcome. 解决这个问题的任何帮助将非常欢迎。 Thanks!
谢谢!
You are missing a space between 1
and AND
: 您缺少
1
到AND
之间的空格:
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
^^^^
HERE
Interestingly enough, you got it right everywhere else. 有趣的是,您在其他任何地方都能做到。
You have to add a space before your AND
. 您必须在
AND
之前添加一个空格。
Try: 尝试:
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
instead of 代替
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
Add Space before AND P.sector = '$sector'
在
AND P.sector = '$sector'
之前添加空格
Change from 从
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
To 至
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
Please give space either: 请给空格:
ON O.id = P.owner WHERE 1 ";
or 要么
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.