Php 未定义的属性:stdClass::$contents在代码点火器中

Php 未定义的属性:stdClass::$contents在代码点火器中,php,html,database,codeigniter,codeigniter-2,Php,Html,Database,Codeigniter,Codeigniter 2,我当时正在学习,并且正在跟随一个关于数据库选择方法的CodeIgniter教程。它的一个方法就是这个 它是一个名为data_Model.php的模型文件: <?php class Data_model extends CI_Model{ function getAll(){ $this->db->select('title','contents'); $this->db->from('data');

我当时正在学习,并且正在跟随一个关于数据库选择方法的CodeIgniter教程。它的一个方法就是这个

它是一个名为data_Model.php的模型文件:

<?php
    class Data_model extends CI_Model{
        function getAll(){
        $this->db->select('title','contents');
        $this->db->from('data');
        $this->db->where('id',1);           
        $q = $this->db->get();

        if ($q->num_rows() > 0){
            foreach($q->result() as $row){
                $data[] = $row;
            }
        }
        return $data;           
    }
}
?>
看起来我的$contents变量没有值,但我非常确定我的数据库中每个对应的条目都有内容。正如下图所示:


我哪里出错了?我遵循的教程没有遇到错误?为什么我会犯这个错误

像这样试试对你有好处

<?php
    class Data_model extends CI_Model{
        function getAll(){
        $this->db->select('title,contents');
        $this->db->from('data');
        $this->db->where('id',1);           
        $q = $this->db->get();

        if ($q->num_rows() > 0){
            return $q->result(); 
        }          
    }
}
?>

和在视图文件中

<html>
    <head>
        <title>
        </title>
        <meta http-equiv="Content-Type" content="text/html"; charset=UTF-8>     
    </head>
    <body>
        <div id="container">
            <p>My view has been loaded.</p>         
            <?php foreach($rows as $r) { ?>
            <h1><?php echo $r->title; ?></h1>
            <div><?php echo $r->contents; ?></div>
            <?php } ?>    
        </div>
    </body>
 </html>

我的视图已加载。


DB active record library
select()
方法只接受第一个参数中可以是数组或字符串的输入查询字段,请参见下文

$this->db->select(array('title','contents'));
//OR
$this->db->select('title,contents');
打印($rows);出口把输出贴在这里。
<?php
    class Data_model extends CI_Model{
        function getAll(){
        $this->db->select('title,contents');
        $this->db->from('data');
        $this->db->where('id',1);           
        $q = $this->db->get();

        if ($q->num_rows() > 0){
            return $q->result(); 
        }          
    }
}
?>
<html>
    <head>
        <title>
        </title>
        <meta http-equiv="Content-Type" content="text/html"; charset=UTF-8>     
    </head>
    <body>
        <div id="container">
            <p>My view has been loaded.</p>         
            <?php foreach($rows as $r) { ?>
            <h1><?php echo $r->title; ?></h1>
            <div><?php echo $r->contents; ?></div>
            <?php } ?>    
        </div>
    </body>
 </html>
$this->db->select(array('title','contents'));
//OR
$this->db->select('title,contents');