简体   繁体   English

尝试在json编码期间获取非对象错误的属性

[英]Trying to get property of non-object error during json encode

Here is a piece of code i am trying to write. 这是我要编写的一段代码。 Here i want to retrieve data from the database, store that into an array and then json_ecode it and set is as output. 在这里,我想从数据库中检索数据,将其存储到数组中,然后对其进行json_ecode并设置为输出。 But i keep on getting this error "Trying to get property of non-object". 但是我继续收到此错误“试图获取非对象的属性”。

Here is the code 这是代码

$data['query'] = $this->db->get('forms');

foreach($data['query'] as $row)
{
    $forms_array[] = array(
        $row->id 
    );
}   

$return=json_encode($forms_array);
$this->output->set_output($return); 

You should first check that your query is actually returning rows. 您应该首先检查您的查询是否实际上正在返回行。 Secondly, you want to access $data['query']->result() in your foreach : 其次,您要在foreach访问$data['query']->result()

if($data['query']->num_rows() > 0){
    // The query returned rows...
    foreach($data['query']->result() as $row){
        $forms_array[] = array(
            $row->id 
        );
    } 
}

您应该在foreach之外声明$ forms_array []。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM