1

我想将 if 语句中产生的所有 use_id_one 值存储在一个数组中。知道怎么做吗?

$query = mysql_query(" SELECT `user_id` FROM users WHERE (surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%') ");

while($run = mysql_fetch_array($query)){
   $user_id_one = $run['user_id']; 

   $check_friend_query = mysql_query("  SELECT friends_id from friends WHERE  (user_one='$session_user_id' AND user_two ='$user_id_one') OR (user_one='$user_id_one' AND user_two='$session_user_id')   ");

    if( mysql_num_rows($check_friend_query) == 1  ){    
         echo"$user_id_one";
        //the above echo prints me all user_id_one values I want
        //how to store all these values of user_id_one in an array

    }
}       

以及接下来如何输出它们,以确保存储的正确性(用于测试目的)

4

2 回答 2

2

替换这一行:

echo"$user_id_one";

用这条线

$array[] = $user_id_one; //this will store the $user_id_one value in the array
于 2013-07-20T13:10:38.573 回答
1

将以下修改添加到代码中以将值添加到数组中

$usersIdArray = array();
$index = 0;
while($run = mysql_fetch_array($query)){
   $user_id_one = $run['user_id']; 

    $check_friend_query = mysql_query("  SELECT friends_id from friends WHERE  (user_one='$session_user_id' AND user_two ='$user_id_one') OR (user_one='$user_id_one' AND user_two='$session_user_id')   ");

    if( mysql_num_rows($check_friend_query) == 1  ){    
        $userIdArray[$index++] = $user_id_one;

    }
}

foreach($userIdArray as $userId) {
    echo $userId;
}
于 2013-07-20T13:11:44.847 回答