- 论坛徽章:
- 0
|
问题基本解决,5分钟可完成。
修改后的代码:
<?php
if ($conn=@mysql_connect("localhost","user","password"))
{
mysql_select_db("test");
$i = 0;
while ($i <= 1000000){
$user_data_array =array();
$sql = "select user_id from users limit ";
$sql.= "$i";
$sql.= ",1000;";
echo $sql;
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query))
{
$user_data_array[] = $row['user_id'];
}
$new_array = array();
foreach ($user_data_array as $uid)
{
$new_array[$uid] = array();
while (count($new_array[$uid])<50)
{
$rand_userid_key = array_rand($user_data_array);
$rand_userid = $user_data_array[$rand_userid_key];
if ($rand_userid!=$uid && !in_array($rand_userid, $new_array[$uid]))
{
$new_array[$uid][] = $rand_userid;
}
}
}
$str = "insert into user_new(user_id, user_friend_id) values";
foreach ($new_array as $uid=>$v)
{
foreach ($v as $partnerid)
{
$str .= "($uid, $partnerid),";
}
}
$sql_insert = substr($str,0, -1);
$hd = fopen('/tmp/data.txt', 'a+');
fwrite($hd, $sql_insert);
$i=$i+1000;
}
fcolse($hd);
mysql_close();
}
else
{
printf("<p>can't connect DB! %s</p> \n",
mysql_error());
}
?>
[ 本帖最后由 r_a 于 2009-1-16 13:37 编辑 ] |
|