Welcome Guest, Not a member yet? Register   Sign In
how codeigniter join two tables to have both Id columns
#1

I want to join two tables that I have both Id columns in join table by codeigniter. I want both id column from comment and users tables.
I write below code

Code:
$this->db->select('users.name as user_full_name, users.id as userid', false);
   $this->db->from('users');

   $this->db->select()
       ->from('comment')
       ->where('project_id', $projectId)
       ->where('user_id', $user_id)
       ->join('users', 'comment.user_id_from =userid')
       ->order_by("comment.id", "asc");
   return $this->db->get()->result_array();

but face error, I do not know why

error:

Code:
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 '* FROM (users, comment) JOIN users ON comment.user_id_from =userid W' at line 1

SELECT users.name as user_full_name, users.id as userid, * FROM (users, comment) JOIN users ON comment.user_id_from =userid WHERE project_id = '3' AND user_id = '84' ORDER BY comment.id ASC

please show me how to solve it
Reply
#2

Example:

stackoverflow - codeigniter join 2 table data
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(03-27-2019, 03:39 AM)InsiteFX Wrote: Example:

stackoverflow - codeigniter join 2 table data

Thank you for your reply I change my code as below. now i access both id from two table as different name
Code:
$this->db->select('*,comment.id as comment_id,users.id as userid');
       $this->db->from('comment');
       $this->db->where('user_id', $user_id);
       $this->db->where('project_id', $projectId);
       $this->db->join('users', 'comment.user_id_from = users.id');
       $this->db->order_by("comment.id", "asc");
       return $this->db->get()->result_array();
Reply




Theme © iAndrew 2016 - Forum software by © MyBB