Search code examples
phpzend-db

Sum of SQL fields in PHP


Hi I'm trying to find the sum of SQL fields in PHP. This sum must be less than or equal to the number which I receive in controller from JavaScript. But my SQL syntax is not working in PHP, instead it fetches all rows from the database.

public function feedbackaverageAction(){                
    $value =$_GET['value'];
    $db = Zend_Registry::get('dbadapter');
    $select = new Zend_Db_Select($db);
    $select = $db->select();
    $select->from(array('feedback' => 'feedback'),
                   array ('customdata',
                          'checkinprocess',
                          'service_by_owner',
                          'locationrating',
                          'cleanlinessrating',
                          'valueformoneyrating',
                          'kitchenequipmentrating'));
    $select->where('feedback.checkinprocess' + 
                   'feedback.service_by_owner' + 
                   'feedback.locationrating' +
                   'feedback.cleanlinessrating' +
                   'feedback.valueformoneyrating' +
                   'feedback.kitchenequipmentrating' <= $value);
    $stmt = $select->query();
    $result = $stmt->fetchAll();
    $this->view->rows = $result;
}

Can anyone help me how to find the sum and check it to the value which I got in the controller.


Solution

  • Try this code and make SQL query like this.

    $value = $_GET['value'];
    $db = Zend_Registry::get('dbadapter');
    $select = new Zend_Db_Select($db);
    $select = $db->select();
    
    $select->from(array('feedback' => 'feedback'),array ('customdata','checkinprocess','service_by_owner','locationrating','cleanlinessrating','valueformoneyrating','kitchenequipmentrating'));
    
    $select->where(new Zend_Db_Expr('feedback.checkinprocess'+'feedback.service_by_owner'+'feedback.locationrating'+'feedback.cleanlinessrating'+'feedback.valueformoneyrating'+'feedback.kitchenequipmentrating')<= $value);
    
    $stmt = $select->query();
    
    $result = $stmt->fetchAll();
    
    $this->view->rows = $result;