0

我已经在这里提出了一个问题。但是由于我没有得到对我有很大帮助的答案,所以我想再问一次。

    In Database [1]
    Table Characters
+----+------------------+-------+---------+
|account_name| lastaccess| online| voted  |
+------------+-----------+-------+--------+
| Account1   | 1231321231|   1   |    1   |
| Account1   | 132312213 |   0   |    0   |
| Account3   | 13231212  |   0   |    0   |
+------------+-----------+-------+--------+
    In Database [2]
    Table Accounts
+----+------------------+
|   Login   | lastIp    | 
+-----------+-----------+
| Account1  | 0.0.0.0.0 |
| Account1  | 0.0.0.0.0 |
| Account3  | 0.0.0.0.0 |
+-----------+-----------+

我已经有一个函数可以获取 lastIP 帐户的位置。

function getclientip()
    {
        if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
            return $_SERVER["REMOTE_ADDR"]; 
        } else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    { 
            return $_SERVER["HTTP_X_FORWARDED_FOR"] ; 
        } else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
            return $_SERVER["HTTP_CLIENT_IP"] ; 
        }
            return "0.0.0.0";
    }
$Ip=getclientip();
$sql='SELECT login FROM accounts WHERE lastIp like \''.$Ip.'\';';
echo mysql_error();
$result = mysql_query($sql);
if (false === $result) {
}
while($row = mysql_fetch_array($result))
{

我想做的是:

mysql_query('update characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1;') ;

MIN(lastaccess)只在哪里。我希望你明白我的意思,在其他情况下问我我会解释得更好。

4

2 回答 2

0

很好,我会跳过你的功能,因为远程地址总是可用的。

$Ip = $_SERVER['REMOTE_ADDR'];
$sql= mysqli_query( $database, "SELECT login FROM accounts WHERE lastIp = '$ip' ");

while($row = mysqli_fetch_assoc($sql)) {
    if(mysqli_query("UPDATE characters SET voted = '1' WHERE account_name = "'.$row['login'].'" AND online = '1'")) {
       print 'updated succesfully';
    } else {
        print 'update did not complete';
    }
}

我不会评论你的行为,我只想说这是你从我这里得到的唯一答案。

于 2013-04-04T22:24:42.887 回答
0

我尝试为您解释我的答案,以便更好地理解并删除易混淆的部分:

我保留了这部分代码:

function getclientip()
    {
        if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
            return $_SERVER["REMOTE_ADDR"]; 
        } else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    { 
            return $_SERVER["HTTP_X_FORWARDED_FOR"] ; 
        } else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
            return $_SERVER["HTTP_CLIENT_IP"] ; 
        }
            return "0.0.0.0";
    }
$Ip=getclientip();

现在您可以将这行 sql 与您的更新代码行连接起来:这行

$sql='SELECT login FROM accounts WHERE lastIp like \''.$Ip.'\';';

这行:

mysql_query('update characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1;') ;

你在你的问题中写的,现在加入这部分的结果是:

$sql='UPDATE characters SET voted=1 where online=1 AND account_name IN (SELECT login FROM accounts WHERE lastIp like \''.$Ip.'\');';

现在你只想更新 MIN(lastaccess) 那么你可以使用这个查询,因为我按 lastaccess 排序升序行,它的平均值从 lastaccess 列的低到高然后选择这行的顶部,换句话说,我选择一个具有最小 lastaccess 的行,然后我将其传递给 set voted=1 的更新命令:

UPDATE characters SET voted=1 where online=1 AND account_name IN
(SELECT login FROM accounts WHERE lastIp like \''.$Ip.'\' ORDER BY lastaccess ASC LIMIT 1);

并且您可以使用临时表并使用 MIN 函数:

UPDATE characters SET voted=1 where online=1 AND account_name IN
(SELECT tempTable.login FROM (SELECT login, MIN(lastaccess) FROM accounts WHERE lastIp like \''.$Ip.'\' GROUP BY login) tempTable;

并且不要忘记您的结果没有任何行,因为它的更新命令..

mysql_query('UPDATE characters SET voted=1 where online=1 AND account_name IN
    (SELECT login FROM accounts WHERE lastIp like \''.$Ip.'\' ORDER BY lastaccess ASC LIMIT 1);');

或者

mysql_query('UPDATE characters SET voted=1 where online=1 AND account_name IN
(SELECT tempTable.login FROM (SELECT login, MIN(lastaccess) FROM accounts WHERE lastIp like \''.$Ip.'\' GROUP BY login) tempTable;');

我希望,是你需要的。

于 2013-04-03T21:08:04.770 回答