Server Problem (MySQL)
#1

Hello,

i'll add on MySQL the IP from any Player when he connects the server.
This is under OnPlayerConnect
PHP код:
// Aliases
    
new query[130 MAX_PLAYER_NAME];
    
format(querysizeof(query), "SELECT * FROM aliases WHERE username = '%s'"PlayerName(playerid));
    new 
result mysql_query(query);
    if(
mysql_num_rows(result) == 0)
    {
        new 
aquery[130 MAX_PLAYER_NAME];
        
format(aquerysizeof(aquery), "INSERT INTO aliases (username, ip) VALUES ('%s', '%s')"PlayerName(playerid), PlayerIP(playerid));
        
mysql_query(aquery);
    } else {
        new 
aquery[130 MAX_PLAYER_NAME];
        
format(aquerysizeof(aquery), "UPDATE aliases SET username = '%s', ip = '%s'"PlayerName(playerid), PlayerIP(playerid));
        
mysql_query(aquery);
    } 
MySQL Info:

PHP код:
id    int(11)            Nein    kein(e)    AUTO_INCREMENT
username    varchar
(40)    latin1_swedish_ci        Nein    kein(e)
ip    varchar(40)    latin1_swedish_ci        Nein    kein(e
PHP код:
stock PlayerName(playerid)
{
    new 
pName[MAX_PLAYER_NAME];
    
GetPlayerName(playeridpNamesizeof(pName));
    return 
pName;
}
stock PlayerIP(playerid)
{
    new 
pIP[26];
    
GetPlayerIp(playeridpIPsizeof(pIP));
    return 
pIP;

It doesn't save my ip when i join the server. There are not any Errors & Warnings on Pawn
Reply
#2

your insert looks ok, does it work for new players?

your update needs to know where, i usually use id field but can do with name i suppose. here...



Код:
    new query[130 + MAX_PLAYER_NAME]; 
    format(query, sizeof(query), "SELECT * FROM aliases WHERE username = '%s'", PlayerName(playerid)); 
    new result = mysql_query(query); 
    if(mysql_num_rows(result) == 0) 
    { 
        new aquery[130 + MAX_PLAYER_NAME]; 
        format(aquery, sizeof(aquery), "INSERT INTO aliases (id, username, ip) VALUES ('0', '%s', '%s')", PlayerName(playerid), PlayerIP(playerid)); 
        mysql_query(aquery); 
    } else { 
        new aquery[130 + MAX_PLAYER_NAME]; 
        format(aquery, sizeof(aquery), "UPDATE aliases SET username = '%s', ip = '%s' WHERE username = '%s'", PlayerName(playerid), PlayerIP(playerid), PlayerName(playerid)); 
        mysql_query(aquery); 
    }
Reply
#3

It doesn't work for new players too :S
But thanks anyway for your answer
Reply
#4

oh cuz it has no ID, try adding parameter id with value 0 (since field is auto increment it must be above 0 so inserting with 0 will add them to the next auto increment instead), ill edit my post above to reflect what i mean
Reply
#5

The id field doesn't really serve a purpose in this case. If you want to insert it anyway, then use default as value.
PHP код:
INSERT INTO aliases (idusernameipVALUES (default, '%s''%s'
A tip from my part though: If you add a unique key on the 'username' field, you can use the INSERT .. ON DUPLICATE KEY UPDATE syntax. This saves you the extra select query.

PHP код:
INSERT INTO aliases (idusernameipVALUES (default, '%s''%s'ON DUPLICATE KEY UPDATE ip '%s'
Reply
#6

Doesn't work -.- i use 2 tables


Tablename: users

id
username
password
register_date
last_login
ip
level
banned
disabled
money
score
kills
deaths
times
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)