简体   繁体   中英

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.

Please advise.

MySQL will return null for sum() if there are no matching records. 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.

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

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