简体   繁体   中英

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. For example, if I put in WHERE 1 with only the condition like sector = 2 , it is working. But when I put the if conditions, it is not working anymore.

$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; 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

Any help resolving this issue will be very welcome. Thanks!

You are missing a space between 1 and 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 .

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'

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'"; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM