Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3935794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:52:16+00:00 2026-05-19T23:52:16+00:00

I’m working on a website, and I’m implementing a friend system. People can become

  • 0

I’m working on a website, and I’m implementing a friend system. People can become friends and that will unlock some stuff which is not important. It is coded in PHP with MySQL as database.

Lets say this is my account table

id | name  | picture
0  | Jorik | Cat.jpg
1  | Joost | Fish.jpg
2  | Henk  | Ferret.png

This is the friend table(Is this a good way to do this?)

id | user id | friend id | invite date | accepted
0  | 0       | 1         | 123         | 0
1  | 2       | 0         | 456         | 1
2  | 1       | 2         | 123         | 1

The way I check if they are friends is as following (Could be an error in it, but you get the idea).

$fid = friend id, $uid = user id.

 WHERE ( (`userid` = '{$uid}' && `friendid` = '{fid}' ) || (`friendid` = '{$uid}' && `userid` = '{$fid}' ))

First of all, is this an efficient way of doing this or is there a better way?

If I wanted to get the list of friends a user has I can run this query

WHERE (`userid` = '{$uid}' || `friendid` = '{$uid}')

now if I would run this query for the user 0, it would return

id | user id | friend id | invite date | accepted
0  | 0       | 1         | 123         | 0
1  | 2       | 0         | 456         | 1

I have worked with MySQL joins before but I can’t figure out how to return something like the following result

id | friend id | invite date | accepted | Name  | Picture 
0  | 1         | 123         | 0        | Joost | Fish.jpg
1  | 2         | 456         | 1        | Jorik | Ferret.png

It would have to check if it should join the friend id or the user id with id from the account table. (The invited person is the friend id, the one who invited him will get the user id.)

I hope I gave enough information.

Can anyone help me with this?

Thanks in advance,

Jorik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:52:16+00:00Added an answer on May 19, 2026 at 11:52 pm

    My $0.02:

    1) Normalize your data: get rid of the either ‘userid’ or ‘id’. You only need one unique column for accounts.

    2) Make a table for ‘friends’ like so:

    ( approximately )
    create table Friends( UserId int unsigned, FriendUserId int unsigned, index( UserId, FriendUserId ));

    Then add a row for each ‘friend’ where ‘FriendUserId’ is the account id (see #1) for each person.

    3) Make a table for ‘invites’ like so:
    (appx)
    create table Invites ( UserId int unsigned, FriendId int unsigned, InviteDate datetime default timestamp, Accepted tinyint(1) default 0, primary key( UserId, FriendId ));

    Add a row for each invite and update if/when its accepted.

    Now to get a persons friends:

    select * from Accounts a join Friends f on a.UserId = f.UserId where a.UserId = ;

    to get Invites:

    select * from Accounts a join Invites i on a.UserId = i.UserId

    And so on…

    .. but more than anything.. avoid duplicating data ..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have some data like this: 1 2 3 4 5 9 2 6
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.