0

I'm trying to get a value from the database inside a controller like this:

//Load model
$this->load->model('files/files_model');        

//Lookup file
$file = $this->files_model->findfilebyid('1');

echo "Filepath: " . $file->filepath . "<br>";
echo "Filename: " . $file->filename . "<br>";

But when I do this, I get a "Trying to get property of non-object" error. I Also tried $file->['filepath'] and $file[0]->filepath, but this does not work.

Printing the array with 'print_r(array_values($file))' gives this:

Array ( [0] => stdClass Object ( [fileid] => 1 [filenamefriendly] => Kiosk manual [filename] => kiosks [filenamepath] => filestorage [fileownerid] => 4 [filepermissiontype] => global [filepermissioncompanyid] => 0 [filecategoryid] => 1 [filecreatedate] => 2015-11-20 00:00:00 [filedescription] => This is the manual of the kiosks [filetype] => pdf ) )

Am I trying to put an array in an array or something? How can I access the values?

4
  • 1
    $file[0]->filepath Does not work? Commented Nov 21, 2015 at 14:00
  • Try casting the $file variable ie $file = (array) $file; var_dump($file) Commented Nov 21, 2015 at 14:02
  • First thing to check is that findfildbyid() is returning something. The error indicates that $file is either NULL or FALSE and not the Object you are expecting. Show us your model code. Commented Nov 21, 2015 at 14:04
  • Hmm; $file[0]->filenamepath work. I should learn to use the right database column names :) However, I'm curious why I need to add [0].. is that because I put an array in an array or something? Commented Nov 21, 2015 at 14:08

1 Answer 1

1

That's because you're returning a full result set (which is an array of objects) While what you actually wants is a single object since the 'id' should be a primary key.

In your findfilebyid it should be something like:

public function findfilebyid ($id) {
       $q = $this->db->where('id', $id)->get('files');
       return $q->row();
}
Sign up to request clarification or add additional context in comments.

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.