简体   繁体   English

mysql_query表示SQL语法错误,但相同的查询在mysql客户端中正常运行

[英]mysql_query says error in sql syntax but the same query works fine in mysql client

When I try to send the following query: 当我尝试发送以下查询时:

SET @attempts=0; 
SELECT mt1.TimeStamp, mt1.ReadAttempts - @attempts AS delta, 
       @attempts := mt1.ReadAttempts ReadAttempts 
FROM OneWireStats mt1 
WHERE SensorIndex=1 ORDER BY mt1.TimeStamp;

to fetch delta values between rows in table OneWireStats I receive following error: 获取表OneWireStats中的行之间的增量值,我收到以下错误:

Query failed with 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 mt1.TimeStamp, mt1.ReadAttempts - @attempts AS delta, @attempts := mt1.Re' at line 1 Query = SET @attempts=0; 检查与您的MySQL服务器版本相对应的手册,以在'SELECT mt1.TimeStamp,mt1.ReadAttempts-@attempts AS delta,@attempts:= mt1.Re'第1行附近使用正确的语法Query = SET @ attempts = 0 ; SELECT mt1.TimeStamp, mt1.ReadAttempts - @attempts AS delta, @attempts := mt1.ReadAttempts ReadAttempts FROM OneWireStats mt1 WHERE SensorIndex=1 ORDER BY mt1.TimeStamp;, Query no: 0 选择mt1.TimeStamp,mt1.ReadAttempts-@尝试AS增量,@ attempts:= mt1.ReadAttempts来自OneWireStats mt1的ReadAttempts,其中SensorIndex = 1由mt1.TimeStamp;排序,查询号:0

When I paste the query from the error printout above into mysql client it works fine. 当我将上述错误打印输出中的查询粘贴到mysql客户端中时,它工作正常。

mysql>  SET @attempts=0; SELECT mt1.TimeStamp, mt1.ReadAttempts - @attempts AS delta, @attempts := mt1.ReadAttempts ReadAttempts FROM OneWireStats mt1 WHERE SensorIndex=1 ORDER BY mt1.TimeStamp LIMIT 10; Query OK, 0 rows affected (0.01 sec)

+---------------------+-------+--------------+ 
| TimeStamp           | delta | ReadAttempts |
+---------------------+-------+--------------+ 
| 2013-12-23 07:55:01 |  5532 |         5532 | 
| 2013-12-23 08:00:00 |   302 |         5834 | 
| 2013-12-23 08:05:01 |   302 |         6136 | 
| 2013-12-23 08:10:00 |   302 |         6438 | 
| 2013-12-23 08:15:00 |   302 |         6740 | 
| 2013-12-23 08:20:01 |   302 |         7042 | 
| 2013-12-23 08:25:00 |   302 |         7344 | 
| 2013-12-23 08:30:01 |   302 |         7646 | 
| 2013-12-23 08:35:00 |   302 |         7948 | 
| 2013-12-23 08:40:01 |   302 |         8250 |
+---------------------+-------+--------------+ 
10 rows in set (0.07 sec)

I use the following php code: 我使用以下php代码:

$query = mysql_real_escape_string(trim($queryobj[$counter]));

$sqlresult = mysql_query($query) or die("Query failed with error: ".mysql_error() . 
             " Query = " . $query . ", Query no: " . $counter);

I do not use any single or double quotes in my query so I do not yet understand the error printout. 我在查询中没有使用任何单引号或双引号,因此我还不了解错误打印输出。

Greatful for hints. 非常有用的提示。

You need to run each query separately, may be like 您需要分别运行每个查询,可能像

mysql_query( "SET @attempts=0" );
mysql_query( "SELECT mt1.TimeStamp, mt1.ReadAttempts - @attempts AS delta, @attempts := mt1.ReadAttempts ReadAttempts FROM OneWireStats mt1 WHERE SensorIndex=1 ORDER BY mt1.TimeStamp" );

OR use mysqli 's multi_query() , like: 或者使用mysqlimulti_query() ,例如:

$query  = "SET @attempts=0;";
$query .= "SELECT mt1.TimeStamp, mt1.ReadAttempts - @attempts AS delta, @attempts := mt1.ReadAttempts ReadAttempts FROM OneWireStats mt1 WHERE SensorIndex=1 ORDER BY mt1.TimeStamp";
mysqli_multi_query($connection, $query);
mysqli_next_result($connection);
if ($result = mysqli_store_result($connection)) {
  //while loop here
}

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

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