$query = "SELECT users FROM tabl ORDER BY RAND()";
$result = mysql_query ($query)
or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');
while ($row = mysql_fetch_array($result)) {
$users[] = $row[0];
}
$current = end($users);
$partners = array();
foreach ($users as $user)
{
$partners[$user] = $current;
$current = $user;
}
print_r($partners);
$query2 = "UPDATE tabl SET partner = {$partners[0]} WHERE users = '$users'";
mysql_query ($query2)
or die ("<br>Query '$query2' failed with error message: \"" . mysql_error () . '"');
这就是我正在使用的代码。查询2之前一切都很好。我已经尝试了我能想到的每一种变化,但没有任何作用。
表上有两个字段:用户和合作伙伴。代码以随机顺序拉动用户,然后将它们分配给一个圆圈中的另一个。我需要使用分配填充合作伙伴字段。
答案 0 :(得分:1)
将更新查询放在foreach循环中,然后您可以提供合作伙伴和用户变量,而不会在以后潜入数组:
foreach ($users as $user)
{
$partners[$user] = $current;
$current = $user;
$query2 = "UPDATE tabl SET partner = '{$partners[$user]}' WHERE users = '{$user}'";
mysql_query ($query2)
or die ("<br>Query '$query2' failed with error message: \"" . mysql_error ()
}
答案 1 :(得分:0)
使用WHERE users IN ('.implode(',',$users).')'
答案 2 :(得分:0)
我会将代码更改为:
$current = end($users);
$partners = array();
foreach ($users as $user)
{
$partners[$user] = $current;
$current = $user;
$query2 = "UPDATE tabl SET partner = {$partners[$user]} WHERE users = '$user'";
mysql_query ($query2)
or die ("<br>Query '$query2' failed with error message: \"" . mysql_error () . '"');
}
print_r($partners);
但是你也可以根据你想要的结果做以下事情:
$userList = join(',', $users);
$query2 = "UPDATE tabl SET partner = {$partners[0]} WHERE users IN ($userList)";
mysql_query ($query2)
or die ("<br>Query '$query2' failed with error message: \"" . mysql_error () . '"');