2

anyone correct this query according to ZF.i have a situation where user select differet parameters and on the base of that make my query

$user = new Zend_Session_Namespace('user');
$phone_service_id = $user->phone_service_id;

$start_date = $this->_getParam('start_date'); //02/07/2012
$end_date = $this->_getParam('end_date');     //02/21/2012
$option_call_log = $this->_getParam('option_call_log'); //COLUMN NAME
$option_call_log_asc_desc = $this->_getParam('option_call_log_asc_desc');  //ASC/DESC

i think i have a syntax error in query ,see it here

$select = $DB->select()
         ->from('CALL_LOG', array('caller_name','call_number','call_start_time','call_duration','call_direction'))
         ->where('phone_service_id = ?', $phone_service_id)
         ->where(DATE_FORMAT(date_created, '%m/%d/%Y') BETWEEN  $start_date  AND $end_date)
         ->order($option_call_log $option_call_log_asc_desc)
         ->limit(0,9);

whats wrong with this ??

6
  • Missing some quotes on ->where(DATE_FORMAT(date_created, '%m/%d/%Y') BETWEEN $start_date AND $end_date)? This should throw a PHP syntax error long before it even gets to querying the db. Shouldn't it be something like ->where("DATE_FORMAT(date_created, '%m/%d/%Y') BETWEEN $start_date AND $end_date")? (note inserted double-quotes) Commented Feb 9, 2012 at 7:15
  • what's the error you got? And try to echo $select and get what query you have found and run it on mysql. Commented Feb 9, 2012 at 7:15
  • In fact, it looks like you need quotes inside the ->order() clause, too. Commented Feb 9, 2012 at 7:16
  • @DavidWeinraub Message: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'AND )' at line 1 Commented Feb 9, 2012 at 7:18
  • @GauravVashishtha the error is above Commented Feb 9, 2012 at 7:20

2 Answers 2

2

The BETWEEN clause is inclusive, the correct way is shown below:

$select = $DB->select()
         ->from('CALL_LOG', array('caller_name','call_number','call_start_time','call_duration','call_direction'))
         ->where('phone_service_id = ?', $phone_service_id)
         ->where("DATE_FORMAT(date_created, '%m/%d/%Y') >= ?",  $start_date)
         ->where("DATE_FORMAT(date_created, '%m/%d/%Y') <= ?",  $end_date)
         ->order("".$option_call_log." ".$option_call_log_asc_desc)
         ->limit(0,9);

try the above code and your problem will be solved.

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

2 Comments

I have make change in the answer. So now check it.
If I'm not mistaken, this will not utilize any index on the date and would require a full table scan.
0

zend db also used in magento so i found out the way.

$sql = $read->select()
                ->from(array('g' => $res->getTableName('geoloc')));

        $whereSql = "INET_ATON('{$remoteAddr}') BETWEEN g.start AND g.end";

        $sql = $sql->where($whereSql); //echo $sql;exit;
        $result = $read->fetchRow($sql);

the output will be like below:

SELECT g.* FROM geoloc AS g WHERE (INET_ATON('41.98.30.186') BETWEEN g.start AND g.end)

hope it helps.

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.