CodeIgniter Forums
friends and users - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: friends and users (/thread-67809.html)

friends and users - anmol - 04-12-2017

-2 down vote favorite

i am making a friend system for a social networking kind of website.i have completed the search part now i am working on adding when a logged in user visits a other guy's profile he can send him friend on clicking the button i am taking two parameter one is friend's id and other is user's id and passing it to the tables are user_login for users and friends have 1)user_id1 2)friend_id1 and so now i want to pass my data that i have recieved after clicking on the add friend button to the database and for that i have to join the two tables.i.e user_login and pivot table thaton clicking these id's get saved in that pivot table.just wanted someone to help me with the joining of tables.since it is showing me error. my code for model is

public function display_followers($user_id,$friend_id)


$this->db->join('user_login', ' = friends.user_id');
$this->db->join('user_login', ' = friends.friend_id');

return $query->Result();

return false;

my code for controller is
public function followers(){

   $user_id = $this->session->userdata['logged_in']['id'];


RE: friends and users - xenomorph1030 - 04-14-2017

You mentioned you receive an error and I don't see any mention of the error. So, let's look at what you have. Your query builder doesn't look correct to me. I believe your query ends up like this:

SELECT * FROM user_login
JOIN user_login ON = friends.user_id
JOIN user_login ON = friends.friend_id
WHERE id = $user_id AND id = $friend_id

There are several things wrong with this. You are joining the same table without giving it an alias, so the columns will be ambigious if the database even allows it. On top of that, your ON statement references users_login instead of user_login or vice versa. I also see no join for a friends table. Then your WHERE clause is saying the ID has to be the $user_id AND the $friend_id. This query simply will not work. Without knowing your table structure, it is difficult for people to help out.

One thing you can do to troubleshoot queries is instead of calling get(), you can do the following:
PHP Code:
$sql $this->db->get_compiled_select();

This will display the actual query that CodeIgniter has compiled. You can run it directly against your database to see more specific results.