1

我有一个使用内部联接进行搜索的查询,但浏览器显示语法消息错误

20你的SQL语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 17 行的 '= '' 或 village = '')' 附近使用正确的语法

谁能告诉我我的查询有什么问题???

询问:

$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());
4

2 回答 2

0

你需要引用你的字符串:

$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());
于 2013-05-19T15:57:34.060 回答
0

您不应该在 SQL 查询中比较 PHP 变量。

不应该是:

$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());
于 2013-05-19T15:58:49.873 回答