简体   繁体   English

SQLSTATE[HY000]:ZF1 中 MySQL 变量的一般错误

[英]SQLSTATE[HY000]: General error with MySQL variable in ZF1

I have a MySQL query that works perfectly in PhpMyAdmin but throws an SQLSTATE[HY000]: General error when using within Zend Framework 1.12 and its fetchAll() method.我有一个 MySQL 查询,它在 PhpMyAdmin 中完美运行,但在 Zend Framework 1.12 及其fetchAll()方法中使用时抛出SQLSTATE[HY000]: General error Here it is:这里是:

SET @csum := 0;
SELECT s1.Date, (@csum := @csum + s1.total) as cumulative_sum
FROM (
        SELECT c.datefield AS Date, IFNULL(SUM(subscription_total),0) AS total
        FROM subscription s
        RIGHT JOIN calendar c
        ON c.datefield = DATE(subscription_date)
        WHERE (c.datefield BETWEEN
            (SELECT MIN(DATE(subscription_date)) FROM subscription)
            AND
            NOW()
        )
        GROUP BY DATE
    ) AS s1

The statement doesn't return any error if I remove the SET statement but I need to set a MySQL variable otherwise cumulative_sum will just be NULL values.如果我删除SET语句,该语句不会返回任何错误,但我需要设置一个 MySQL 变量,否则cumulative_sum将只是NULL值。 Here is the code of the method:下面是该方法的代码:

public function findCumulativeCashflow($statut)
{
    $db = $this->_getDbTable();

    $dbAdapter = new Zend_Db_Adapter_Pdo_Mysql($db->getAdapter()->getConfig());

    $sql = '<SQL statement above>';

    $statement = $dbAdapter->query($sql);

    $rows = $statement->fetchAll();

    return $rows; 
}

The error points the $rows = $statement->fetchAll();错误指向$rows = $statement->fetchAll(); line, is there another ZF method to use with SET @var := value ?行,是否有另一种 ZF 方法与SET @var := value

Any ideas would be much appreciated.任何想法将不胜感激。 Thanks!谢谢!

As per this question , I need to use another query as fetchAll() cannot handle multiple queries.根据这个问题,我需要使用另一个查询,因为 fetchAll() 无法处理多个查询。

I therefore need to set the MySQL variable with $dbAdapter->query('SET @csum := 0;');因此,我需要使用$dbAdapter->query('SET @csum := 0;');设置 MySQL 变量$dbAdapter->query('SET @csum := 0;'); and then do the main query.然后做主查询。

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

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