MIike Eps
MIike Eps

Reputation: 441

Trying to get property 'join_id' of non-object

please advice me on how to resolve this issue with error I get from my PHP CodeIgniter API

error Trying to get property 'join_id' of non-object

query

Select * from join_chat where
(user_1 = '24' and user_2 = '26') or
(user_1 = '26' and user_2 = '24')

result of query
+---------+--------+--------+
| join_id | user_1 | user_2 |
+---------+--------+--------+
|      20 | 26     | 24     |
+---------+--------+--------+
1 row in set (0.01 sec)

From the result, you can see that the join id is returned as 20, yet I get the error

Trying to get property 'join_id' of non-object

see the controller method below

 public function get_join_user()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('post_user_id', 'post_user_id', 'trim|required');
        $this->form_validation->set_rules('login_user_id', 'login_user_id', 'trim|required');
        if ($this->form_validation->run() == false) {
            $data["responce"] = false;
            $data["error"] = 'field is required';
        } else {
            $this->load->model("common_model");
            $q = $this->db->query("Select * from join_chat where
                      (user_1 = '".$this->input->post("login_user_id")."' and user_2 = '".$this->input->post("post_user_id")."') or
                      (user_1 = '".$this->input->post("post_user_id")."' and user_2 = '".$this->input->post("login_user_id")."')");
            //$row = $q->row();
            $row = $q->result();
            if (count($row) > 0) {
                $data["responce"] = true;
                //*****error causing line below***
                $data["data"] = $row->join_id;
            } else {
                $add_chat = array(
                                   "user_1"=>$this->input->post("post_user_id"),
                                   "user_2"=>$this->input->post("login_user_id")
                                   );

                $this->db->insert("join_chat", $add_chat);

                $insertid = $this->db->insert_id();
                if ($insertid) {
                    $data["responce"] = true;
                    $data["data"] = $insertid;
                }
            }
        }
        echo json_encode($data);
    }

line causing the error

$data["data"] = $row->join_id;

Thanks in advance

Upvotes: 1

Views: 53

Answers (1)

sauhardnc
sauhardnc

Reputation: 1961

Again @Mlike Eps, now that you've changed your code from $q->row(); to $row = $q->result(); reference: prev_question. Now it's an array of object, so you'll have to get the data accordingly.

$data["data"] = $row[0]->join_id; //because, now array[0] contains the previous information. 

If by any chance it has more than one array, use foreach() to retrieve data. Hope it helps you. :)

Upvotes: 1

Related Questions