简体   繁体   中英

MySQL - CodeIgniter Fails to Interpolate Query Parameters

Ok I'm running a query and CodeIgniter is giving me this error:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4

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

I do realize, it's not catching the sender, HOWEVER $this->session->userdata('user_id') echos to be 4 (which is my user_id)... So how come it's not showing up as anything?

$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;
}

I tried putting everything on one line because I didn't know if the enters would break anything, but it still didn't fix any of it.

You get an error because you should use the num_rows on the query result, not on the database. Also, you query binding is wrong.

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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