【问题标题】:What does this "Message: Undefined property: stdClass" mean?这个“消息:未定义的属性:stdClass”是什么意思?
【发布时间】:2015-01-16 15:31:38
【问题描述】:

我是 codeigniter 和 php 的新手,我现在遇到这个错误,我不知道我哪里出错了。我应该在我的 foreach 上添加一些东西吗?上次我遇到的是一个未定义的属性,由于我的模型中的拼写错误现在是 stdClass?这次没有单词拼错。有人能告诉我这个问题吗?

错误:

遇到了 PHP 错误

严重性:通知

消息:未定义属性:stdClass::$blockcode

文件名:views/v_schedule.php

行号:10

视图:v_schedule.php

<?php foreach ($query as $row){ ?>


                       <?php echo $row->blockcode;?> <br>
                       <?php echo $row->subjectcode;?> <br>
                       <?php echo $row->daystart;?> <br>
                       <?php echo $row->dayend;?>   <br>
                       <?php echo $row->stime;?>    <br>
                       <?php echo $row->sday;?> <br>
                       <?php echo $row->instructorname;?>   <br>
                       <?php echo $row->instuctorlastname;?>    <br>
                       <?php echo $row->roomcode;?> <br>
                       <?php echo $row->building;?> <br>

        <?php } ?>  

控制器:c_test.php

 function getSchedule() {
       $data['query'] = $this->m_test->result_getSchedule();
       $this->load->view('v_schedule',$data);
 }

型号:m_test.php

function result_getSchedule()

    {
        $this->db->select('grades.studentid', 'grades.blockcode', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');
        $this->db->from('grades');
        $this->db->join('subjectblocking', 'grades.blockcode=subjectblocking.blockcode');
        $this->db->join('instructorinfo', 'subjectblocking.instructorid=instructorinfo.instructorid');
        $this->db->join('room', 'subjectblocking.roomcode= room.roomcode');
        $this->db->distinct();
        $this->db->where('studentid', '2013-F0218');
        $query=$this->db->get();
        return $query->result();
    }

【问题讨论】:

  • 听起来$row 不包含名为blockcode 的属性。尝试在 foreach 循环中添加 print_r($row); echo "&lt;br /&gt;&lt;br /&gt;"; 以查看每个 $row 值包含的内容。您可能错过了该属性...
  • 你也可以在这里php.net/manual/en/errorfunc.constants.php查看不同类型的php错误。
  • 如果你用结果值加载你的m_test 然后传递一个模型实例在控制器中查看会更好。最好让它成为 MVC。
  • @War10ck 它告诉我 array(1) { [0]=> object(stdClass)#23 (1) { ["studentid"]=> string(10) "2013-F0218" } }
  • @AsimAwan 我以为我在做的是 mvc。模型是我放置所有查询的地方,我的 foreach 只是调用我想在我的数据库中检索的选定行

标签: php codeigniter


【解决方案1】:

你错了

$this->db->select('grades.studentid', 'grades.blockcode', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');
//this is is not right way codeigniter select.
//it only selects studentid
//blockcode is not seleted that's why you got that error

这是正确的方法

 $this->db->select('grades.studentid , grades.blockcode , subjectblocking.subjectcode , subjectblocking.daystart , subjectblocking.dayend , subjectblocking.stime , subjectblocking.sday , instructorinfo.firstname , instructorinfo.lastname ,  subjectblocking.roomcode , room.building');

希望这能解决您的问题

【讨论】:

    【解决方案2】:

    成绩和科目阻止表中有两个阻止代码列。

    您应该像这样更改 m_test.php 中的 sql:

     $this->db->select('grades.studentid', 'grades.blockcode as block_code', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');
    

    您应该在 v_schedule.php 中将 $row->blockcode 变量更改为 $row->block_code

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      首先我也使用了上面的代码,我得到了同样的错误。

      $this->db->select('grades.studentid', 'grades.blockcode as block_code', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');
      

      但是通过改变

      $this->db->select('grades.studentid, grades.blockcode as block_code, subjectblocking.subjectcode,subjectblocking.daystart,subjectblocking.dayend, subjectblocking.stime,subjectblocking.sday,instructorinfo.firstname, instructorinfo.lastname, subjectblocking.roomcode,room.building');
      

      我解决了这个错误。

      如果我们可以将grades.studentid 命名为studentID,那么我们可以直接调用:

      echo $query->studentID;
      

      【讨论】:

        猜你喜欢
        • 2013-12-26
        • 2023-03-24
        • 2019-06-15
        • 1970-01-01
        • 1970-01-01
        • 2010-10-01
        • 2015-06-22
        • 1970-01-01
        • 2019-06-12
        相关资源
        最近更新 更多