php Codeigniter 消息:未定义的属性:stdClass
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9792814/
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
Codeigniter Message: Undefined property: stdClass
提问by Linus
I'm trying to query the db with the following query in the model:
我正在尝试使用模型中的以下查询来查询数据库:
function showSpecific(){
$all = $this->session->all_userdata();
$name = $all['u_name'];
$id = $this->db
->select('u_id')
->from('users')
->where('u_name', $name)
->get()
->row();
$id = $id->u_id;
$data = $this->db->query("SELECT messages.m_id, messages.post_id, messages.m_betreff, messages.an_user, messages.von_user, messages.m_time, users.u_id, users.u_name, posts.p_titel, users.u_id, messages.m_content
FROM messages
INNER JOIN users
ON messages.von_user=users.u_id
INNER JOIN posts
ON messages.post_id=posts.post_id
WHERE messages.an_user='$id'
ORDER BY messages.m_time DESC");
return $data;
}
But when I try to output the result in the view:
但是当我尝试在视图中输出结果时:
<?php foreach ($query->result() as $row)
{
echo '<tr><td>';
echo anchor('users/showUser/'.$row->u_id, $row->u_name);
echo '</td><td>';
echo $row->m_betreff;
echo '</td><td>';
echo $row->m_content;
echo '</td><td>';
echo anchor('posts/showSpecific/'.$row->post_id, $row->p_titel);
echo '</td><td>';
echo $row->m_time;
echo '</td></tr>';
}
?>
Only the m_content gets this error: Why is it the only one? And how do I solve this?
只有 m_content 得到这个错误:为什么它是唯一的?我该如何解决这个问题?
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$m_content
Filename: views/specific_message.php
Line Number: 48
Here is my Controller:
这是我的控制器:
function showSpecific(){
$this->load->model('messages_model');
$data['query'] = $this->messages_model->getMessages();
$data['main_content'] = 'specific_message';
$this->load->view('includes/login_template', $data);
}
Thanks a lot!
非常感谢!
回答by The Alpha
I think
我认为
$data['query'] = $this->db->query("...");
should be
应该
$query=$this->db->query("...");
so you can
这样你就可以
foreach ($query->result() as $row) {...}
reference:codeigniter
参考:codeigniter
After question Update :You have $query variable inside your view because of $data['query'] ($data will be extracted), $query is your object now (inside your view) so you can loop the $query.
问题更新后:由于 $data['query'](将提取 $data),您的视图中有 $query 变量,$query 现在是您的对象(在您的视图中),因此您可以循环 $query。
Wrong model name getMessages()has been called, it should be showSpecific()
错误的模型名称getMessages()被调用,它应该是showSpecific()