简体   繁体   中英

What is wrong with this MySQL query syntax?

$db = mysql_select_db("remote"); 

if(!$db) {
    die("Unable to select database");
} 

$query = "SET @cumulative_sum := 0; 
          SELECT timestamp, 
                 @cumulative_sum := @cumulative_sum + value AS cumulative_sum 
          FROM remote.historical 
          WHERE timestamp>= CURDATE()"; 

$result = mysql_query($qry);

When I use this query in my HeidiSQL, it output okay...running cumulative over current day, but when I copied this code into php file, web browser outputs:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT timestamp, @cumulative_sum := @cumulative_sum + value AS cumulative_sum F' at line 1

What is wrong with this code?

Instead of executing two statements, you can alternative declare the variable in a subquery, eg.

SELECT  timestamp, 
        @cumulative_sum := @cumulative_sum + value AS cumulative_sum 
FROM    remote.historical, (SELECT @cumulative_sum := 0) b
WHERE timestamp>= CURDATE()

For security reasons query() function do not allow multiple queries in the single statement but you can do this using mysqli_multi_query() .

The following link will help you: Multiple queries in single PHP query statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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