我有以下php脚本,它选择并在数组中插入我想要选择的所有user_ids。
$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 = $run['user_id'];
$check_friend_query = mysql_query(" SELECT friends_id from friends WHERE (user_one='$session_user_id' AND user_two ='$user_id') OR (user_one='$user_id' AND user_two='$session_user_id') ");
if( mysql_num_rows($check_friend_query) == 1 ){
$array[] = $user_id;
}
}
通过使用以下脚本,我获得了数组中所有用户id的输出:
foreach($array as $key => $value) {
echo "$value </br>";
}
在我的情况下,这个输出打印出5个用户ID:32,36,37,38,39。
我想要的是在以下sql查询中使用这些值:
$sql = " SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE ((surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%')) AND `user_id`='$value' ";
但是如果像上面那样写,则查询仅适用于一个值,在我的情况下适用于39而不是全部。我怎样才能让它适用于所有人?
答案 0 :(得分:0)
尝试将sql语句放在foreach循环中,这样它将为数组中的每个项执行命令:
foreach($array as $key => $value) {
echo "$value </br>";
$sql = " SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE ((surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%')) AND `user_id`='$value' ";
}
我不确定你是否还想输出值 - 如果你不需要它们,那么就把echo命令拿出来。
答案 1 :(得分:0)
将查询放入循环
foreach($array as $key => $value) {
$sql = " SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE ((surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%')) AND `user_id`='$value' ";
}
答案 2 :(得分:0)
你能告诉我你把$ sql放在哪里吗?如果你把它放在循环中,它会在fisnishes时得到最后一个id的值为39。