[英]Trying to get property 'join_id' of non-object
请就如何解决这个问题向我提出建议
错误尝试获取非对象的属性“join_id”
询问
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)
从结果中,您可以看到 join id 返回为 20,但我得到了错误
Trying to get property 'join_id' of non-object
请参阅下面的 controller 方法
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);
}
导致错误的行
$data["data"] = $row->join_id;
提前致谢
再次@Mlike Eps,现在您已将代码从$q->row();
到$row = $q->result();
参考: prev_question 。 现在它是一个 object 数组,因此您必须相应地获取数据。
$data["data"] = $row[0]->join_id; //because, now array[0] contains the previous information.
如果它有多个数组,请使用foreach()
检索数据。 希望它可以帮助你。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.