redGREENblue
redGREENblue

Reputation: 3126

Plain SQL to Codeigniter syntax

I am facing a little trouble with converting this sql to codeigniter syntax. I can use this plain sql and getting results but I have to use return $this->db->query($sql, array($param))->result(); which I guess doesn't return an array (Not sure though, but i keep getting a error "Cannot use object of type stdClass as array in...", and I have no idea if that query can be modified to return an array or any other workaround is available.) Anyway, I guess the best thing to do for me is to follow the CI syntax of query and then use a return $query->result_array(); to get a result array from the query. I know it may be very basic stuff , but somehow I am not able to figure out how to exactly convert this sql to CI syntax. Any help would be appreciated. Thanks. Here is the sql below.

SELECT dirmast.entryID,dirmast.entryTitle,dirmast.entryShortDesc,dirsec.dirsecRefID 
FROM dirmast,dirsec 
WHERE dirsec.drtext = 'something' 
AND dirsec.dirsecRefID = dirmast.entryID 
GROUP BY dirsec.dirsecRefID

Upvotes: 1

Views: 944

Answers (2)

Joseph Silber
Joseph Silber

Reputation: 219920

If you want your results in an array, you do ->result_array(), if you want it as an object, you do ->result(). Either way, you can use both Active Records and query().

Upvotes: 1

james_bond
james_bond

Reputation: 6908

I think this will do what you want:

$this->db->select('dirmast.entryID,dirmast.entryTitle,dirmast.entryShortDesc,dirsec.dirsecRefID');
$this->db->from('dirmast');
$this->db->join('dirsec','dirsec.dirsecRefID = dirmast.entryID');
$this->db->where('dirsec.drtext','something');
$this->db->group_by('dirsec.dirsecRefID');

Upvotes: 3

Related Questions