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

  • Home
  • SEARCH
  • 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 6322241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:17:43+00:00 2026-05-24T16:17:43+00:00

I have an application to add friends. I need to have my script to

  • 0

I have an application to add friends. I need to have my script to check, if the users’ already friends. I thought I could do this by a COUNT. I did like this:

            $username = $_GET[user];
            $ven_til_id = $_SESSION['userID'];

            $num = 1;
            if(isset($_GET['add_friend'])){
                $check=("SELECT username,ven_til_id, COUNT(*) AS num FROM friends WHERE username=$username AND ven_til_id=$ven_til_id GROUP BY username,ven_til_id")or die(mysql_error());
                $result=mysql_query($check);
                    if(!$result){
                        echo "Cant run query.";
                    } else {
                        $num = mysql_num_rows($result);
                    }
                    if($num>0){
                        header("Location: /profil/$username?add_error");
                } else {
                $sql=mysql_query("INSERT INTO friends (username,ven_til_id)VALUES('$username', '$ven_til_id')")or die(mysql_error());
                header("Location: /profil/$username");
                }
            }

            ?>

But when I’m adding one friend it’s fine. It adds it and everything is fine. But then when I try to add another, it says we’re already friends. I guess it’s because it’s counting how many times my ID (ven_til_id) is listed in the tables.

  • 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-24T16:17:45+00:00Added an answer on May 24, 2026 at 4:17 pm

    You’re missing a comma:

    SELECT
     username,ven_til_id COUNT(*) AS num  FROM ...
    

    should be

    SELECT
     username,ven_til_id, COUNT(*) AS num  FROM ...
    

    Also, your reference to the count field is incorrect – it should be the third column or $row[2]

    You may want to make your code more robust by referring to fields by name eg $row['num']

    One final thing to confirm is that the value being retrieved with a count is being treated as an integer not a string. I don’t think it’s the problem here but you may want to explicitly cast it to avoid possible issues later eg…

    $num = (int) $row[2];
    

    Option 1
    Just select the appropriate rows and see how many records you get back…

    SELECT  username,
        ven_til_id
    FROM    friends
    WHERE   username=$username
    AND     ven_til_id=$ven_til_id
    

    Then just count the number of records returned using PHP – eg mysql_num_rows() (I think that’s the correct function name)

    Clarification:
    Change

                        $row = mysql_fetch_row($result);
                        $num = $row[2];
    

    to

                        $num = mysql_num_rows($result);
    

    Option 2

    Get MySQL to do the counting for you – in qhich case you need to tell it to group multiple record together…

    SELECT  username,
        ven_til_id,
        COUNT(*) as Num
    FROM    friends
    WHERE   username=$username
    AND     ven_til_id=$ven_til_id
    GROUP BY username,
        ven_til_id
    

    Then just read the 3rd value of the first row (num) and you’ll have a count

    NB: The second method may be overkill if you’re only ever expecting a 1 or a 0

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

Sidebar

Related Questions

We have an application that allows users to add/edit/replace/delete content (text, images, swfs, mp3s,
I'd like to have my application be able to add bookmarks to safari programmatically,
I'm working on a Rails application where I have users that can add each
My application allows users to chat with friends (like skype or whatsapp), so I
i have application with Listbox and files, each time i press on Add button
I have an application where the user can write up some questions and add
i have one application which is build using VB.NET now i required to add
We have subclassed the Silverlight Application class to add some additional functionality and then
I have a PHP application using an Oracle XE database. Whenever I add a
I have the following application: I have 1 window. On that window I add

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.