22

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"

public 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 asset name value returned back to the controller.

0

5 Answers 5

54

Use row() like,

return $this->db->get()->row()->name;
Sign up to request clarification or add additional context in comments.

2 Comments

I need this data in model here this is my query
$query = $this->db->query("SELECT * FROM users WHERE useremail = '$email' AND usertype = 1 AND usertype = 2")->row()->useremail;
11

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

Comments

4

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;

Comments

1

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)

Comments

1

Try this code of block , I already checked and works fine:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.