显示错误:尝试获取非对象的属性

时间:2013-02-22 05:51:42

标签: php json codeigniter

代码在第8,9,10,11行的控制器中显示错误,试图获取非对象的属性 //检查了所有列名和表名。

我的模特是

public function getEducationDetail($educationID) {
    $this->db->select('qual_id,pass_year,institute,percentage,emp_id');
    $this->db->from('emp_edu');
    $this->db->where('edu_id', $educationID);
    $this->db->where('del_flag', 0);
    $query = $this->db->get();
    if ($query->num_rows() > 0):
        return $query->result();
    else:
        return 0;
    endif;
}

我的控制器是

 public function getEducationDetail() {

    $this->load->model('mod_admin');
    if (isset($_GET['param'])):
        $educationID = $this->input->get('param');
        $result = $this->mod_admin->getEducationDetail($educationID);
        $jsonArr = array(
            'qualificationID' => $result->qual_id,
            'passOut' => $result->pass_out,
            'institute' => $result->institute,
            'percentage' => $result->percentage
        );
    endif;
    $json = json_encode(array('result' => $jsonArr));
    echo $json;
}

3 个答案:

答案 0 :(得分:3)

如果您的模型返回单行$query->row(),那么

$result = $this->mod_admin->getEducationDetail($educationID);
if (!empty($result)) {
    $jsonArr = array(
        'qualificationID' => $result->qual_id,
        'passOut' => $result->pass_out,
        'institute' => $result->institute,
        'percentage' => $result->percentage
    );
}

如果它返回多行$query->result(),那么

$results = $this->mod_admin->getEducationDetail($educationID);

$jsonArr = array();

if (!empty($results)) {
    foreach ($results as $result) {
        $jsonArr[] = array(
            'qualificationID' => $result->qual_id,
            'passOut' => $result->pass_out,
            'institute' => $result->institute,
            'percentage' => $result->percentage
        );
    }
}

希望这有帮助。

答案 1 :(得分:2)

返回$query->result()它会给出多维数组。我想你会得到一行结果。 将其更改为return $query->row()

答案 2 :(得分:0)

模型

public function getEducationDetail($educationID) {
    $this->db->select('qual_id,pass_year,institute,percentage,emp_id');
    $this->db->where('edu_id', $educationID);
    $this->db->where('del_flag', 0);
    $query = $this->db->get('emp_edu');

       return $query->row();

}

控制器

public function getEducationDetail() {

$this->load->model('mod_admin');
if (isset($_GET['param'])):
    $educationID = $this->input->get('param');
    $result = $this->mod_admin->getEducationDetail($educationID);
if($result){
    $jsonArr = array(
        'qualificationID' => $result->qual_id,
        'passOut' => $result->pass_out,
        'institute' => $result->institute,
        'percentage' => $result->percentage
    );
$json = json_encode(array('result' => $jsonArr));
echo $json;
endif;
   }
}