简体   繁体   English

从数组中的控制器中获取数据库值

[英]Get db value from within controller in array

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. 我也尝试了$ file-> ['filepath']和$ file [0]-> filepath,但这不起作用。

Printing the array with 'print_r(array_values($file))' gives this: 用'print_r(array_values($ file))'打印数组得到以下结果:

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? 如何访问这些值?

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. 那是因为您要返回一个完整的结果集(是一个对象数组),而您真正想要的是一个对象,因为'id'应该是主键。

In your findfilebyid it should be something like: 在您的findfilebyid它应该类似于:

public function findfilebyid ($id) {
       $q = $this->db->where('id', $id)->get('files');
       return $q->row();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM