Codeigniter - Object of class __PHP_Incomplete_Class could not be converted to string

狂风中的少年 提交于 2019-12-25 05:18:13

问题


I'm getting a Object of class __PHP_Incomplete_Class could not be converted to string error when I echo my is_active variable, I can work just fine with the username and is_logged_in variable, but not with the is_active variable, I'm wondering what I could be doing wrong here...

On my controller I did:

$username=$this->input->post("username");
            $activated_val=$this->membership_model->is_activated($username);

            $data = array(
                "username" => $this->input->post("username"),
                "is_logged_in" => true,
                "is_active" => $activated_val
            );
            $this->session->set_userdata($data);
            redirect("main");

My model function:

function is_activated($username){
        $query = "SELECT activated FROM members WHERE username=?";
        $result = $this->db->query($query, $username);

        return $result;
    }

And in my view:

$is_active= $this->session->userdata("is_active");
                echo $is_active;

回答1:


In your model change the return line from

return $result;

to

return $result->row()->activated;

The query returns a CodeIgniter object which is not even a result yet, after passing it to row() you get an object with your database column values as this object attributes, then you refer to the activated column value by getting(->activated) the corresponding attribute of this object.



来源:https://stackoverflow.com/questions/12631555/codeigniter-object-of-class-php-incomplete-class-could-not-be-converted-to-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!