小编典典

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

sql

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

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

非常感谢!


阅读 240

收藏
2021-03-17

共1个答案

小编典典

我认为

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

应该

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

这样你就可以

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

参考: codeigniter

问题更新之后: 由于$ data [‘query’](将提取$ data),您的视图内有$ query变量,现在$
query是您的对象(在视图内),因此可以循环$ query。

调用了错误的模型名称 getMessages() ,应为 showSpecific()

2021-03-17