问题
the following function is supposed to read the name of the give asset code from the database. but it triggers the error: "Trying to get property of non-object"
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get()->result()->row('name');
}
All I want is to have the name of the asset returned back to the controller! Your help is highly appreciated!
回答1:
Use row()
like,
return $this->db->get()->row()->name;
回答2:
Use row()
for a single row, and result()
for multiple rows.
回答3:
do like this, asset_types is your table name
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get('asset_types');
}
And in your controller acess it like
$result=$this->modelname->sban_name('$asset')->row();
$name=$result->name;
来源:https://stackoverflow.com/questions/16954107/getting-the-value-of-the-single-field-output-using-the-codeigniter-active-record