0

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.

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fatal error: Call to undefined method CI_DB_mysql_driver::num_rows() in H:\WD SmartWare.swstor\HALEY-HP\Source\DStable\dragon\models\user_model.php on line 351 I did try that before posting this! I guess I should have included that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.