getting the value of the single field output using the codeigniter active record

我是研究僧i 提交于 2020-01-01 04:03:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!