Codeigniter消息:未定义的属性:stdClass

时间:2012-03-20 18:31:50

标签: php sql codeigniter

我正在尝试使用模型中的以下查询来查询数据库:

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;
}

但是当我尝试在视图中输出结果时:

<?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>';
                }
                ?>

只有m_content会出现此错误:为什么它是唯一的错误?我该如何解决这个问题?

A PHP Error was encountered
Severity: Notice

Message: Undefined property: stdClass::$m_content

Filename: views/specific_message.php

Line Number: 48

这是我的控制器:

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);
}

非常感谢!

1 个答案:

答案 0 :(得分:6)

我认为

$data['query'] = $this->db->query("...");

应该是

$query=$this->db->query("...");

所以你可以

foreach ($query->result() as $row) {...}

参考: codeigniter

问题更新后:由于$ data ['query']($ data将被提取),您的视图中有$ query变量,$ query现在是您的对象(在您的视图中)所以你可以循环$查询。

已调用错误的型号名称 getMessages(),它应该是 showSpecific()