Server Problem (MySQL) -
Unstoppable - 29.09.2012
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(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 (username, ip) VALUES ('%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'", 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(playerid, pName, sizeof(pName));
return pName;
}
stock PlayerIP(playerid)
{
new pIP[26];
GetPlayerIp(playerid, pIP, sizeof(pIP));
return pIP;
}
It doesn't save my ip when i join the server. There are not any Errors & Warnings on Pawn
Re: Server Problem (MySQL) -
Necro[ABK] - 29.09.2012
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);
}
Re: Server Problem (MySQL) -
Unstoppable - 29.09.2012
It doesn't work for new players too :S
But thanks anyway for your answer
Re: Server Problem (MySQL) -
Necro[ABK] - 29.09.2012
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
Re: Server Problem (MySQL) -
Vince - 29.09.2012
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 (id, username, ip) VALUES (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 (id, username, ip) VALUES (default, '%s', '%s') ON DUPLICATE KEY UPDATE ip = '%s';
Re: Server Problem (MySQL) -
Unstoppable - 29.09.2012
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