0

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Right - seeing the actual type's of those values may be of use as well.
Variables evaluated just fine, i am running resulting queries directly in database and getting the results. Also, i removed SUMs and left real columns to select... still empty arrays
Here is actual evaluation:string(10) "2011-08-24" string(10) "2011-08-24" int(913)
Which database adapter are you using. If you run $db->fetchAll($query) instead of query, fetchAll do you get any difference?
I'm using PDO. $db->fetchAll($query) did not make any difference, i even ran plain PHP in this action (mysql_connect... etc) - the same result. I am really confused at this point
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.