简体   繁体   English

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

[英]SQL syntax error only when executing a query in PHP

I believe to have a problem with PHP. 我相信PHP会有问题。 I am shown the following error: 出现以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT @n := @n + 1 as count , interests.id , entity.id , entity.name ' at line 2 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第2行的'SELECT @n:= @ n + 1作为count,interests.id,entity.id,entity.name'附近使用

If I execute the the following query in MySql, it does so successfully and returns the desired results. 如果我在MySql中执行以下查询,它将成功执行并返回所需结果。 However, this does not occur when I execute the same query with PHP. 但是,当我使用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

NOTE OF INTEREST: The web server i am using does not support MySqli, therefore I am using mysql_query() functions. 趣味性:我正在使用的Web服务器不支持MySqli,因此我正在使用mysql_query()函数。 I suspect that this could be part of the problem. 我怀疑这可能是问题的一部分。 However, I execute it with MySql WAMP for localhost testing and receive the same error. 但是,我使用MySql WAMP执行它以进行localhost测试,并收到相同的错误。

You can't run multiple queries in one API call. 您不能在一个API调用中运行多个查询。

Simple solution: use multiple API calls. 简单的解决方案:使用多个API调用。 One to SET @n=0 , then a separate API call to execute the SELECT query. 一个是SET @n=0 ,然后是一个单独的API调用以执行SELECT查询。 As long as you use the same connection to MySQL, the value of @n stays alive in the session. 只要你使用MySQL的同一个连接,价值@n停留在会议活着。

The mysqli extension does support mysqli_multi_query() but I still recommend doing one query at a time. mysqli扩展确实支持mysqli_multi_query()但我仍然建议一次执行一个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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