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

后端 未结 4 712
你的背包
你的背包 2020-12-16 11:46

the following function is supposed to read the name of the given asset code from the database. but it triggers the error: \"Trying to get property of non-object\"

         


        
相关标签:
4条回答
  • 2020-12-16 12:07

    Use row() for a single row, and result() for multiple rows.

    0 讨论(0)
  • 2020-12-16 12:20

    Use row() like,

    return $this->db->get()->row()->name;
    
    0 讨论(0)
  • 2020-12-16 12:26

    I think it's important to check if the record that satisfies the conditions even exists in the database. Code for the model:

    function sban_name($asset){
        $this->db->select('name');
        $this->db->from('asset_types');
        $this->db->where('code',$asset);
        $row = $this->db->get()->row();
        if (isset($row)) {
            return $row->name;
        } else {
            return false;
        }
    }
    

    Simply call the function from the controller like so:

     $response = $this->model_name->sban_name($asset)
    
    0 讨论(0)
  • 2020-12-16 12:32

    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;
    
    0 讨论(0)
提交回复
热议问题