How to correct this mysql_query, use the right syntax and make it work?

孤街醉人 提交于 2019-12-11 20:37:51

问题


I've already made a question about this here. But since i didn't get answer that helped me a lot i would like to ask again.

    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 |
+-----------+-----------+

I've already got a function that gets where lastIP account.

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))
{

What I want do is:

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

But where MIN(lastaccess) only. I hope you get my mind, in other case ask me i will explain better.


回答1:


i try to explain my answer for you, for better understanding and remove confusable parts:

i kept this part of your code:

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();

now you can join this line of sql with your update code line: its meens this line

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

and this line:

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

that you wrote in your question,now the result of joining this parts is:

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

now you want update only where MIN(lastaccess) then you can use this query, for that i order ascending rows by lastaccess, its mean from lower to higher of lastaccess column and then select top of this rows, in other word, i select a row that have minimum lastaccess and then i passed it to update command for 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);

AND you can use a temp table and using MIN function:

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;

and don`t forget that your result have not any rows because its update command..

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);');

or

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;');

i hope, is what you need.




回答2:


very well i will skip your function since REMOTE ADRES is always available.

$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';
    }
}

I will not comment on your behavior, all i say is this is the only answer you get from me.



来源:https://stackoverflow.com/questions/15798035/how-to-correct-this-mysql-query-use-the-right-syntax-and-make-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!