0

I'm fairly new to PHP and SQL. I have spent today programming a friend system (almost identical to Facebook's) but I am unsure of how I would have multiple friends in the same column. So lets say the user 'admin' has a friend called 'john' admin sends test a request and john accepts, they both go into each others friends column. But lets say another of admin friends sends him a request and he accepts it will simply overwrite 'john' with the new name, therefore only having one value in the column instead of john, another friend, etc. How would it be possible to not overwrite, but instead keep adding to the column.

I hope this makes sense and thanks in advance!

3
  • multiple friends in the same column. That's not how Facebook does it. Please read up on normalization. Commented Jan 18, 2013 at 19:32
  • FB has a one-to-many relationship. They have a users table and then, say, a user_friends table with two columns: user1id | user2id. Very crude example. But it is most certainly not storing all the friends in a single column in the users table. Commented Jan 18, 2013 at 19:32
  • Ok, thanks for the read. Eli, how would I go about doing this?! Thanks Commented Jan 18, 2013 at 19:36

1 Answer 1

1

What you need is a "One to Many" relationship. One 'person' can be friends with many other 'persons'.

You would have one table called person:

person_id | person_name
-----------------------
1 | John
2 | Frank
3 | Mary
4 | Oscar

And you would have a one-to-many table called friends:

person_id1 | person_id2
-----------------------------------
 1 | 2
 1 | 3
 2 | 3

So, now by looking in our friends table. Our first relationship is between John and Frank, they are friends. So is John and Mary, and Frank and Mary. Oscar has no friends.

Sign up to request clarification or add additional context in comments.

1 Comment

friend_id is unnecessary. You create a composite primary key of person_id1 and person_id2 to prevent duplicate "friendships"

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.