deviloper
deviloper

Reputation: 7240

Query resulting empty in Codeigniter

I have piece of PHP code written in Codeigniter framework that returns nothing (an empty set). Have a look at it and tell me what is wrong with it.

function sbsn($serial){
    $this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
    $this->db->from('asset_types,asset_brands,assets');
    $this->db->where('assets.type_code','asset_types.code');
    $this->db->where('assets.brand_code','asset_brands.code');
    $this->db->where('serial_no',$serial); 
    $result = $this->db->get();
    return $result;
}

Upvotes: 1

Views: 4003

Answers (3)

deviloper
deviloper

Reputation: 7240

The problem can be easily solved by joining the tables in the following way.

$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial); 
$result = $this->db->get()->result_array();
return $result;

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

change:

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

to

$result = $this->db->get()->row_array();  //to get single row
//OR
$result = $this->db->get()->result_array();  //to get multiple rows

OR try using JOIN, like

$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial); 
$result = $this->db->get()->result_array();
return $result;

Upvotes: 1

umefarooq
umefarooq

Reputation: 4574

hi with db get you are only getting result set not record to get results from result you have to the following thing

$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array

you can use $result to perform the same above things
$result->result();
$result->row();

for more information read generating result user guide

Upvotes: 3

Related Questions