1

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;
}
2
  • Have you run the equivalent query in your database console? In CodeIgniter, can you turn on query logging to ensure the statement is being sent to the database server as you intend? Commented Feb 17, 2013 at 10:16
  • Are your Where statements correct? Your checking if assets.type_code is a string of 'asset_types.code' Commented Feb 17, 2013 at 14:11

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

2 Comments

although it is identical to using: $result->result() as $sth in view file, but yet it doesnt work sudhir!
try doing echo $this->db->last_query(); after get(); to check if your sql is like you want it to be ..?
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.