Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - Codeigniter Message: Undefined property: stdClass

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?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think

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

should be

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

so you can

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

reference: 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.

Wrong model name getMessages() has been called, it should be showSpecific()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...