LebDev
LebDev

Reputation: 467

syantax error using php mysql and inner join

i have a query for searching using inner join but the browser display syntax message error

20You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '' or village = '')' at line 17

can anyone tel me what is wrong with my query???

query:

$sql = mysql_query("SELECT user_id,first_name, last_name, birth_date, registered_date, 
    s.specialization_name,
    g.governorate_name,
    d.district_name,
    v.village_name 
          FROM members u
                       INNER JOIN  specialization s 
                        ON u.specialization = s.specialization_id
                        INNER JOIN governorate g
                        ON u.governorate = g.governorate_id
                        INNER JOIN districts d
                        ON u.district = d.district_id
                        INNER JOIN village v
                        ON u.village = v.id
                       where (governorate = ''or governorate = '$bygov') or
                             (district = '' or district = '$bydist') or
                             (village = '' or village = '$byvillage')")
                             or die(mysql_error());

Upvotes: 1

Views: 82

Answers (2)

sdespont
sdespont

Reputation: 14025

You should not compare PHP variable in SQL query.

Shouldn't be :

$sql = mysql_query("SELECT user_id,first_name, last_name, birth_date, registered_date, 
    s.specialization_name,
    g.governorate_name,
    d.district_name,
    v.village_name 
          FROM members u
                       INNER JOIN  specialization s 
                        ON u.specialization = s.specialization_id
                        INNER JOIN governorate g
                        ON u.governorate = g.governorate_id
                        INNER JOIN districts d
                        ON u.district = d.district_id
                        INNER JOIN village v
                        ON u.village = v.id
                       where (governorate= '' or governorate = '$bygov') and
                             (district= '' or district = '$bydist') and
                             (village= '' or village = '$byvillage')")
                             or die(mysql_error());

Upvotes: 0

arheops
arheops

Reputation: 15259

You need quote your strings:

$sql = mysql_query("SELECT user_id,first_name, last_name, birth_date, registered_date, 
    s.specialization_name,
    g.governorate_name,
    d.district_name,
    v.village_name 
          FROM members u
                       INNER JOIN  specialization s 
                        ON u.specialization = s.specialization_id
                        INNER JOIN governorate g
                        ON u.governorate = g.governorate_id
                        INNER JOIN districts d
                        ON u.district = d.district_id
                        INNER JOIN village v
                        ON u.village = v.id
                       where ('$bygov' = '' or governorate = '$bygov') and
                             ('$bydist' = '' or district = '$bydist') and
                             ('$byvillage' = '' or village = '$byvillage')")
                             or die(mysql_error());

Upvotes: 0

Related Questions