I want to make a friends list in my online game. I am not sure how to set it up or where to start. The add friends and accept friends part I can handle, but I don't know how to set it up in mysql and php. A list of users (friends) connected to each user or something?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
If your friendship relationship is symmetrical, you can either store each pair in a separate record:
and query all
B
's friends like that:or store the user with the least
id
in the first field and that with the greatestid
in the second one:and query
B
's friends like that:The first option is a little bit more efficient in
MySQL
, and this is the only option if your friendship relationship is not symmetrical (like onLiveJournal
)See this article:
A friendship is essentially a mutual relationship between two people. In database terms it's a many-to-many relationship between two users.
So what you need is a linking table that holds references to two users by ID.
Example
Users table
Friends table
This would make Bob friends Jim and Jim friends with Alice.
Check out the answers in this other post here on stackoverflow...some simple but great explanations to accomplishing what you need.
Facebook database design?
An alternative would be to set column A as follow: $checkfriend = (($friend1 $friend2) || ($friend2 $friend1))
Column B as follow: (friendship accepted or friendship rejected) based on user's choice
Basically have both user1 and user2 in the same column.