0

好的,我正在运行一个查询,CodeIgniter 给了我这个错误:

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的 '' 附近使用正确的语法

SELECT username FROMfriend_request JOIN user ON user_id =friend WHERE (friend = '8' AND sender =

我确实意识到,它没有捕捉到发件人,但是 $this->session->userdata('user_id') 回显为 4(这是我的 user_id)......那么它为什么没有显示出来呢?

$this->db->query('SELECT username
    FROM friend_request
    JOIN user ON user_id = friend
    WHERE (friend = ? AND sender = ?)
    OR (friend = ? AND sender = ?)', 
        $user->row()->user_id, 
        $this->session->userdata('user_id'), 
        $this->session->userdata('user_id'), 
        $user->row()->user_id);

if($this->db->num_rows() > 0) {
    $this->errors->set_error('You either have a pending request from '.ucfirst($this->db->row()->username).' 
        or you have already requested their friendship!');
    return false;
}

我尝试将所有内容放在一条线上,因为我不知道输入是否会破坏任何内容,但它仍然没有解决任何问题。

4

1 回答 1

3

你会得到一个错误,因为你应该num_rows在查询结果上使用,而不是在数据库上。另外,您的查询绑定是错误的。

$query = $this->db->query(
  'SELECT username
  FROM friend_request
  JOIN user ON user_id = friend
  WHERE (friend = ? AND sender = ?)
  OR (friend = ? AND sender = ?)', 
  array(
    $user->row()->user_id, 
    $this->session->userdata('user_id'), 
    $this->session->userdata('user_id'), 
    $user->row()->user_id
  )
);

if ($query->num_rows() > 0) {
    $this->errors->set_error('You either have a pending request from '.ucfirst($this->db->row()->username).' 
        or you have already requested their friendship!');
    return false;
}
于 2013-01-28T17:40:56.503 回答