0

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.

Community
  • 1
  • 1
  • If you already asked the question, I would suggest providing a link to it. – David Starkey Apr 03 '13 at 21:04
  • Note*The man that answer was correct, but i was too sleepy to attention his questions. Anyway i'm actualy not enough skilled to solved it by myself. So thats why i re-ask. Link: http://stackoverflow.com/questions/15779416/error-while-trying-to-check – Unique Power Apr 03 '13 at 21:06
  • @JW already gave you a valid answer including a version embedding SQL in PHP. Please try that answer and report the error. – koriander Apr 03 '13 at 21:19
  • Assuming the error is the one you provided below, it does not make sense to do an update query and then fetch results. I suspect you are formulating your question incorrectly. It does not make sense the replace a SELECT command with an UPDATE. – koriander Apr 03 '13 at 21:41
  • I'm confuzed, you guys confuzed me... this doesn't help @ all! All i wanna do is `mysql_query('update characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1;') ;` but where MIN(lastaccess) ONLY, thats all... – Unique Power Apr 03 '13 at 21:54
  • So, why do you want to run a query that updates records and then loop on the records (line 50)? The query given by JW is correct according to your explanations. – koriander Apr 04 '13 at 12:16

2 Answers2

0

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.

Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42
  • `mysql_query('update characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1;')` ; But where MIN(lastaccess) only. and both tables are in differend databases. So nop. This wont work. At all i wanna make `voted=1` only to 1 character from this login, and not to all. So i'm trying to use MIN(lastaccess) as a point, to update character vote=1 only where MIN(lastaccess) – Unique Power Apr 03 '13 at 21:10
  • Does anyone have any other idias? :D – Unique Power Apr 03 '13 at 21:20
  • @JW code The error i get is `Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\vote\vote.php on line 50 ` and line 50 is `while($row = mysql_fetch_array($result))` – Unique Power Apr 03 '13 at 21:28
  • ` `lastAccess` bigint(13) unsigned NOT NULL DEFAULT '0', ` – Unique Power Apr 03 '13 at 21:41
  • I'm confuzed, you guys confuzed me... this doesn't help @ all! All i wanna do is `mysql_query('update characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1;') ;` but where MIN(lastaccess) ONLY, thats all... – Unique Power Apr 03 '13 at 21:57
  • Unique Power, I agree with you, the answer above from @MehdiYeganeh has nothing to do with what you asked and it just confuses things. – koriander Apr 04 '13 at 12:13
0

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.

Patrick Aleman
  • 612
  • 6
  • 19