Salvatore Fucito
Salvatore Fucito

Reputation: 355

Group By cause non object

I'm using codeigniter and I'm trying to perform a query execution passing a where clause, this where is an array, this is my code:

$now = date('Y-m-d H:i:s');
$this->record_model->getAllRecord(array('guid' => $guid,
                                 'end_datetime <' => $now,
                                 'GROUP BY hash'));

this is the content of getAllRecord

$query = $this->db
                ->select('*')
                ->from('appointments')
                ->where($where_clause) 'is passed as parameter..
                ->get()->result_array();

this is the content of where

array(3) {
["guid"]=>
string(36) "c3682d6f-75b2-4686-966d-cb3c9344369b"
["end_datetime <"]=>
string(19) "2016-01-21 12:03:58"
[0]=>
string(13) "GROUP BY hash"
}

Now if I remove the Group by all working well, but if I insert the Group By hash for don't have (duplicate) in the result this error appear:

Call to a member function result_array() on a non-object

Essentially I want get the information of all records but some records have the same hash, so the GROUP BY should join this record in one. But I did something wrong...

Upvotes: 0

Views: 45

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Try this

$query = $this->db->select('*')
            ->where($where_clause)
            ->get('appointments');
$result = $query->result_array();
return $result;

Upvotes: 0

Shadow
Shadow

Reputation: 34232

Group by is not part of the where clause, therefore codeigniter will generate a syntactically incorrect code, therefore ->get() call does generate a proper resultset. You should add the group by clause using a separate ->group_by(...) call to your query.

Upvotes: 1

Related Questions