PHP 错误:试图获取非对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21051836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Error: Trying to get property of non-object
提问by Tom
I am working in CodeIgniter framework and I have tried to apply all the other solutions I found on stack but I could not make it work so here is my problem... I am trying to retrieve a record from MySQL database table called 'questions' based on uri segment. Then I am trying to display this record in a view. As far as I can tell, the uri segment is passed along everywhere it needs to be, and record is retrieved from database. The problem comes up when I am trying to access the data from the controller, in my view. Error I am getting for each echo in my loop in 'view_thread_view' is
我在 CodeIgniter 框架中工作,我试图应用我在堆栈上找到的所有其他解决方案,但我无法使其工作,所以这是我的问题......我正在尝试从名为“问题”的 MySQL 数据库表中检索记录基于uri段。然后我试图在视图中显示这条记录。据我所知,uri 段被传递到它需要的任何地方,并从数据库中检索记录。在我看来,当我尝试从控制器访问数据时,问题就出现了。我在“view_thread_view”的循环中为每个回声得到的错误是
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/view_thread_view.php
遇到 PHP 错误
严重性:注意
消息:试图获取非对象的属性
文件名:views/view_thread_view.php
Any solutions would be greatly appreciated.
任何解决方案将不胜感激。
Here is my code:
这是我的代码:
Controller thread
控制器线程
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
Model data_model
模型数据_模型
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , 'qid' , $quest));
{
return $q;
}
}
View view_thread_view
查看 view_thread_view
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
</div>
EDIT: QUESTION ANSWERED
编辑:问题已回答
Thanks to Girish Jangid fixed the problem this is the working code:
感谢 Girish Jangid 解决了这个问题,这是工作代码:
Controller
控制器
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
//$data = array();
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
}
Model
模型
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , array('qid' => $quest)));
{
return $q;
}
}
View
看法
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $data->uname
?>
</div>
<?php endforeach; ?>
</div>
</div>
回答by Girish
You should user db function to fetch results, please use CodeIgniter db Query Results functions, like this
你应该使用 db 函数来获取结果,请使用 CodeIgniter db Query Results 函数,像这样
if($records->num_rows() > 0){
foreach($records->result() as $data){
if(isset($data->title)) echo $data->title;
}
}
For more detail please read CodeIgniter Query Result function Query Results
更多细节请阅读 CodeIgniter 查询结果函数 查询结果
Try this code
试试这个代码
<div title="question_view">
<h1>Question View<h1>
<?php if($records->num_rows() > 0) { ?>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
回答by Jessica
$recordsis an array, not an object, therefore you cannot do $records->uname. You probably meant $datathere too.
$records是一个数组,而不是一个对象,因此你不能做$records->uname。你可能$data也是那个意思。
Edit: On second look, it appears $datais an array as well! Arrays are accessed via ['key']not ->key
编辑:再看,它似乎$data也是一个数组!数组是通过['key']not访问的->key
Also, your code is way over complicated.
此外,您的代码过于复杂。
<?php
foreach($records as $data){
$string = <<<STR
<div>
<h2>Title</h2>
{$data['title']}
</div>
...etc
STR;
echo $string;
}

