存储在数组中生成的值

时间:2013-07-20 13:06:17

标签: php

我想在数组中存储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

    }
}       

以及如何输出它们,以确保存储的correclty(用于测试目的)

2 个答案:

答案 0 :(得分:2)

替换此行:

echo"$user_id_one";

这一行

$array[] = $user_id_one; //this will store the $user_id_one value in the array

答案 1 :(得分: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;
}