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 9031523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:45:11+00:00 2026-06-16T07:45:11+00:00

Continuing this question , in my web app, I want to allow users to

  • 0

Continuing this question,

in my web app, I want to allow users to add friends, like facebook, in my previous question, I finally decided to have the database structure as @yiding said:

I would de-normalize the relation such that it’s symmetric. That is,
if 1 and 2 are friends, i’d have two rows (1,2) and (2,1).

The disadvantage is that it’s twice the size, and you have to do 2
writes when forming and breaking friendships. The advantage is all
your read queries are simpler. This is probably a good trade-off
because most of the time you are reading instead of writing.

This has the added advantage that if you eventually outgrow one
database and decide to do user-sharding, you don’t have to traverse
every other db shard to find out who a person’s friends are.

So, now if user 1 adds user 2, and user 5 adds 2, something like this will go into the db:

ROW_ID    USER_ID   FRIEND_ID   STATUS
1         1         2           0
2         2         1           0
3         5         2           0
4         2         5           0

As you see, we insert the row of the “REQUEST SENDER” first, so now imagine that user 5 is logged in, and we want to show him the friendship requests, here is my query:

$check_requests = mysql_query("SELECT * FROM friends_tbl WHERE FRIEND_ID = '5'");

the above query, will fetch ROW_ID = 4, this means with the above query shows us that user 2 has added 5, but he has NOT, actually the user 5 added user 2, so here we should not show any friendship requests for user 5, instead we need to show it for user 2.

How I’m supposed to check this correctly?

  • 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-06-16T07:45:12+00:00Added an answer on June 16, 2026 at 7:45 am

    This is an edited answer.


    Your SQL query should look like this:

    SELECT USER_ID, FRIEND_ID FROM friends_tbl WHERE FRIEND_ID = '5' OR USER_ID = '5'
    

    Then you have to parse your result in this way. Assuming you have got a php array like this:

    $result = array(
        0 => array(
            'USER_ID' => 5,
            'FRIEND_ID' => 2
        ),
        1 => array(
            'USER_ID' => 2,
            'FRIEND_ID' => 5
        )
        2 => array(
            'USER_ID' => 5,
            'FRIEND_ID' => 8
        ),
        3 => array(
            'USER_ID' => 8,
            'FRIEND_ID' => 5
        )
    )
    

    You just have to get the even rows:

    $result_final = array();
    for($i = 0; $i < count($result); $i++) {
        if($i % 2 == 0) $result_final[] = $result[$i];
    }
    

    Then you will have an array like this:

        $result = array(
        0 => array(
            'USER_ID' => 5,
            'FRIEND_ID' => 2
        ),
        1 => array(
            'USER_ID' => 5,
            'FRIEND_ID' => 8
        )
    )
    

    Alternative method: Make your SQL look like this:

    SELECT FRIEND_ID FROM friends_tbl WHERE USER_ID = '5'
    

    That’s all.

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

Sidebar

Related Questions

For a web app I want to let users review and edit a record
Continuing from this question : When I am trying to do fopen on Windows,
I have a list like this: ['Ww','Aa','Bb','Cc','ww','AA','BB','CC'] And continuing in such a pattern, with
This is a general design question about how to make a web application that
I see in this question that WCF Web API is still in preview and
I am continuing my previous question , as the reply lead to further doubts/points/concerns.
I have a web-app that allows users to upload and download image files by
This question is a follow up to a previous question I had I'm trying
I hate to be posting a question like this on here, but I'm running
I'm relatively new to web application programming so I hope this question isn't too

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.