0
votes

I have a problem regarding codeigniters db->get(). I have functioning sql query, when i pass it to db "manually", i get result, but when i use codeigniter, i get empty result. Almost same query but with like instead of where works.

The SQL is:

SELECT se.Id, se.Name, se.Alias, sy.Name as System, os.Name as Os, ac.Name as Access, se.Description
FROM cdl_server se
INNER JOIN cdl_system sy on sy.Id = se.System
INNER JOIN cdl_os os on os.Id = se.Os
INNER JOIN cdl_access ac on ac.Id = se.Access
WHERE se.Id = 1

The code looks like this:

    $this->db->select(
            'se.Id, se.Name, se.Alias, ' . 
            'sy.Name as System, os.Name as Os, ac.Name as Access, ' . 
            'se.Description', 
            FALSE);
    $this->db->from('cdl_server se');
    $this->db->join('cdl_system sy', 'sy.Id = se.System' ,'inner', FALSE);
    $this->db->join('cdl_os os', 'os.Id = se.Os' ,'inner', FALSE);
    $this->db->join('cdl_access ac', 'ac.Id = se.Access' ,'inner', FALSE);
    $this->db->where('se.Id', $id, FALSE);
    $this->db->get();

edit: I use it like: $query = $this->db->get(); return $query->result();

When i var_dump the sql query before it is sent to the database, i get this:

string(264) "SELECT se.Id, se.Name, se.Alias, sy.Name as System, os.Name as Os, ac.Name as Access, se.Description FROM cdl_server se INNER JOIN cdl_system sy ON sy.Id = se.System INNER JOIN cdl_os os ON os.Id = se.Os INNER JOIN cdl_access ac ON ac.Id = se.Access WHERE se.Id =1" 

But result is always empty, even though when i copy the above and try asking db directly, i get result i need.

Does anyone know what could be causing it?

1
$this->db->join doesn't accept a fourth parameter. Did you try $this->db->get()->result()? - Yan Berk
Thank you very much, that works - but i wonder why does this work and $query = $this->db->get(); return $query->result() did not. Thanks again! - Luga
You need to use the db library to generate a result. $query is just a simple string. It doesn't have functions such as result(). - Yan Berk
this worked for me untill now, thats why im puzzled. - Luga
if you're using $q = $this->db->get(); return $q->result(); and still getting no results then your SQL statement clearly isn't defined properly. - gorelative

1 Answers

4
votes

If u want to get result you must use row() or result() function.

$this->db->get()->result();