仅在PHP中执行查询时出现SQL语法错误

时间:2017-01-21 18:26:19

标签: php mysql

我相信PHP存在问题。我出现以下错误:

  

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,使用在' SELECT @n:= @n + 1附近作为count,interests.id,entity.id,entity.name'在第2行

如果我在MySql中执行以下查询,它会成功执行并返回所需的结果。但是,当我使用PHP执行相同的查询时,不会发生这种情况。

 SET @n = 0 ;
    SELECT @n := @n + 1   as count
      , interests.id
      , entity.id
      , entity.name
      , entity.entity_type
      , entity.city_country_province
      , entity.street_address
      , entity.user_id
      , entity.descript
      , entity.icon
      , city_country_province_type.city
      , city_country_province_type.province
      , city_country_province_type.country
      , entity.linkout
      , entity.map_lng
      , entity.map_lat              
    FROM interests INNER JOIN interest_entity ON interests.id = interest_entity.interet_id
    INNER JOIN entity ON interest_entity.entity_id = entity.id
    INNER JOIN city_country_province_type ON entity.city_country_province = city_country_province_type.id             
    WHERE ((interests.id)=9) 
    GROUP BY interests.id
      , entity.id
      , entity.name
      , entity.entity_type
      , entity.city_country_province
      , entity.street_address
      , entity.user_id
      , entity.descript
      , entity.icon
      , city_country_province_type.city
      , city_country_province_type.province
      , city_country_province_type.country
      , entity.linkout
      , entity.map_lng
      , entity.map_lat   
      HAVING count >= 10 AND count <= 20 order by entity.name desc

利息注释:我使用的网络服务器不支持MySqli,因此我使用的是mysql_query()函数。我怀疑这可能是问题的一部分。但是,我使用MySql WAMP执行它以进行localhost测试并收到相同的错误。

1 个答案:

答案 0 :(得分:1)

您无法在一次API调用中运行多个查询。

简单的解决方案:使用多个API调用。一到SET @n=0,然后执行SELECT查询的单独API调用。只要您使用与MySQL相同的连接,@n的值在会话中保持活动状态。

mysqli扩展程序支持mysqli_multi_query(),但我仍然建议您一次执行一个查询。