让php脚本多次运行

I have a system where a PHP script uses MySQL to get info based on a user. Then, based on that information, a certain button will be displayed. The database that is being called has columns:

id
user_one
user_two

This is meant to check if two users are friends. However, my problem is that if a user has more that 1 friend the script only works for the 1st friend.

$select_friends_query = mysql_query("SELECT friend_id FROM friends WHERE user_id = '$user'");
 while($friend_row = mysql_fetch_assoc($select_friends_query)) {
$friend = $friend_row['friend_id'];
}

if ($username == $friend) {
 $addAsFriend = '<input type="submit" class = "frnd_req" name="removefriend" value="Disassociate">';
}

else
{
 $addAsFriend = '<input type = "submit" class = "frnd_req" name = "addfriend" value = "Send Associate Request">';
 }
}
}

Then I have echo $addAsFriend later.

You have to look over all results. Like the while loop example in http://www.php.net/manual/en/function.mysql-fetch-assoc.php

I recommend that you change your database design as follows:

User_id, PK
Friend_id, PK

PK = Primary Key. Primary key is the key under which records are stored. It must be unique. The reason we are making it a COMPOUND primary key (two fields make up the PK instead of 1) is because it is impossible for a user to be friends with with the same user Multiple times. Mysql will ensure this does not happen and you won't have to do it on the application level.

Thus, if user "12" and user "25" become friend you should two records:

(12, 25) and (25, 12)

You must have two records because data means literally "user this has friend that." Is it possible for two users to have a one way friendship - not really BUT you may want to one day expand this table to include preferences on the relationship type between the two friends and you would want to distinguish between A -> B and B -> A relationship.

So let's get to the meat of the question. To query mysql to find all friends to a specific user we do the following:

$sql = "SELECT friend_id FROM friends WHERE user_id = 25;";
$query = mysql_query($sql, $connection);

// Loop through all friend records
while ($row = mysql_fetch_assoc($query)) {

   $friends[$row['friend']];
}

I don't use procedural code (mysql_query) and instead use mysqli with OP: $mysql->query(). basically, I am not 100% sure if this code will run but it gives you a guide to get started.

At the end of the program, you will have an array "friends" with keys that tell you the friend ids. So "friends" -> 12, 21 could be a potential data set.