简体   繁体   English

zend框架中的查询返回空数组

[英]Query in zend framework returns empty array

i am running a custom query, no models involved, and it return an empty array. 我正在运行自定义查询,没有涉及模型,它返回一个空数组。

Here is exact code that i use: 这是我使用的确切代码:

$query = 'SELECT SUM(open_diff) opens, SUM(revenue_diff) revenue, SUM(revenue) real_rev, manual_rev, SUM(opens) actual_opens 
                         FROM data.discrepancy
                         WHERE discrepancy_date >= \''.$dateStart.'\' AND discrepancy_date <= \''.$dateEnd.'\' AND  feed_id = '.$feeds[$i]["feed_id"];

                $db = Zend_Registry::get('db_slave');
                $stmt = $db->query($query);

               $records = $stmt->fetchAll();


Zend_Debug::dump($records); gets me this result:

array(1) {
[0] => array(5) {
["opens"] => NULL
["revenue"] => NULL
["real_rev"] => NULL
["manual_rev"] => NULL
["actual_opens"] => NULL
}
}

The data is in the database, and if i run that query directly in MySql, i have no problems. 数据在数据库中,如果我直接在MySql中运行该查询,我没有问题。

Please advise. 请指教。

MySQL will return null for sum() if there are no matching records. 如果没有匹配的记录,MySQL将为sum()返回null。 What is the final query being executed (with the variables evaluated)? 正在执行的最终查询是什么(评估变量)?

I'd try running that in MySQL directly as well and you'll probably get the same results. 我也尝试直接在MySQL中运行它,你可能会得到相同的结果。

var_dump($dateStart, $dateEnd, $feeds[$i]['feed_id']); 

See what those contain and you'll probably see the problem. 看看那些包含什么,你可能会看到问题。

Try following code 请尝试以下代码

$dbAbstract = new Zend_Db_Table_Abstract();

$select = $dbAbstract->select()
    ->from(
          array('a' => 'data.discrepancy'),
          array('SUM(open_diff) AS opens', 'SUM(revenue_diff) AS revenue'), 'SUM(revenue) AS real_rev', 'SUM(opens) AS actual_opens'
    )
    ->where('discrepancy_date >=?', $dateStart)
    ->where('discrepancy_date <=?', $dateEnd)
    ->where('feed_id =?', $feeds[$i]["feed_id"]);

$result = $dbAbstract->fetchAll($select);

Please remove AS that is not working 请删除无效的AS

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

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