MySQL 在 WHERE 子句中使用 if 条件时出现语法错误
MySQL syntax error on using if condition in WHERE clause
当我在 Mysql 查询的 WHERE
子句中使用 if
条件时出现语法错误。例如,如果我在 WHERE 1
中输入 sector = 2
这样的条件,它就可以工作。但是当我输入 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'"; }
这是错误:
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 '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
非常欢迎任何解决此问题的帮助。谢谢!
您在 1
和 AND
之间缺少一个 space:
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
^^^^
HERE
有趣的是,你在其他地方都做对了。
您必须在 AND
之前添加一个 space。
尝试:
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
而不是
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
在AND P.sector = '$sector'
前添加Space
变化自
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
到
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
请给出space:
ON O.id = P.owner WHERE 1 ";
或
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
当我在 Mysql 查询的 WHERE
子句中使用 if
条件时出现语法错误。例如,如果我在 WHERE 1
中输入 sector = 2
这样的条件,它就可以工作。但是当我输入 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'"; }
这是错误:
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 '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
非常欢迎任何解决此问题的帮助。谢谢!
您在 1
和 AND
之间缺少一个 space:
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
^^^^
HERE
有趣的是,你在其他地方都做对了。
您必须在 AND
之前添加一个 space。
尝试:
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
而不是
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
在AND P.sector = '$sector'
变化自
if (!empty($sector)) { $query .= "AND P.sector = '$sector'"; }
到
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }
请给出space:
ON O.id = P.owner WHERE 1 ";
或
if (!empty($sector)) { $query .= " AND P.sector = '$sector'"; }