首页 > 解决方案 > codeigniter:试图获取非对象的属性

问题描述

严重性:通知消息:尝试获取非对象的属性文件名:admin/search_result.php 行号:73

我正在尝试从费用表中的费用列中检索数据。我不知道代码有什么问题。

                          <?php
                            $term_fee = $this->db->get_where('fees' , array(
                              'student_id' => $row['student_code'], 'term' => $running_term,
                              'year' => $running_year ))->row()->fees;
                            echo $term_fee;
                              ?>

这是我的看法。需要帮忙

标签: codeigniter

解决方案


该错误意味着您的查询失败。你不应该假设它会成功。这样做的方法是在使用任何生成查询结果的函数之前检查调用的结果。

我最好的猜测是您提供的值之一不可用。因此,请仔细检查您的输入值。确保它们在那里并且是你所期望的。

尽管此代码可能无法修复结果,但至少您捕获了失败的查询,并且将获得有关已完成操作的更有意义的信息。

<?php
$query = $this->db->get_where('fees', array(
    'student_id' => $row['student_code'], 
    'term' => $running_term,
    'year' => $running_year )
    );

if(isset($query))
{
    $term_fee = $query->row()->fees;
    echo $term_fee; 
}
else
{
   //do something about the failed query
    echo "Query Failed. Here's what you asked the database to do:<br>";
    echo $this->db->last_query();
}

当“get family”函数失败时会发生什么,它返回 FALSE 并且 FALSE 不是具有任何属性的“对象”。它当然没有row()附加功能。因此,在使用任何查询结果函数之前,请务必检查什么get()get_where()返回。


推荐阅读