AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Function Help (https://forums.alliedmods.net/showthread.php?t=141020)

IchWill 10-18-2010 16:22

Function Help
 
Hi.
I've done a plugin that logs nick, authid,ip to a database ...
But when the player join again, the ip etc is inserted again, then i have duplicate results...
Can you help with a function that checks before doing the query if the nickname is in the database?
Here is partial code:
Code:

public client_putinserver(id){


new Error[512], Handle: SqlTuple;
new host[64], user[64], password[64], database[64], prefix[64];
new configsDir[64]
get_configsdir(configsDir, 63)
server_cmd("exec %s/amxx.cfg", configsDir)        // Execute main configuration file
server_cmd("exec %s/sql.cfg", configsDir)
get_cvar_string("mpstats_sql_host", host, charsmax(host));
get_cvar_string("mpstats_sql_user", user, charsmax(user));
get_cvar_string("mpstats_sql_pass", password, charsmax(password));
get_cvar_string("mpstats_sql_db", database, charsmax(database));
get_cvar_string("mpstats_sql_prefix", prefix, charsmax(prefix));
SqlTuple = SQL_MakeDbTuple(host, user, password, database);


new Handle: SqlConnection, Handle: Query, ErrorCode;
SqlConnection = SQL_Connect(SqlTuple, ErrorCode, Error, charsmax(Error));
new name[32],protocol[32],userip[32];
get_user_name(id,name,31);
get_user_ip(id, userip, 31, 1);
get_user_authid(id, protocol,31);
if(SqlConnection != Empty_Handle)
Query = SQL_PrepareQuery(SqlConnection, "INSERT INTO `mostplay_players`(`nick`,`protocol`,`ip`,`active`) VALUES ('%s','%s','%s','1')", name,protocol, userip); 
SQL_Execute(Query);
SQL_FreeHandle(Query);
SQL_FreeHandle(SqlConnection);
server_cmd("echo Player %s with ip %s connected!",name, userip)
return PLUGIN_CONTINUE;
}

Cheers.(and sorry for my bad english)

Schwabba 10-19-2010 03:23

Re: Function Help
 
You have to check if there is an entry with that steamid before you send an insert.

Means:

PHP Code:

Query SQL_PrepareQuery(SqlConnection"SELECT * FROM `mostplay_players` WHERE (`mostplay_players`.`protocol` = '%s')"protocol

Then you have to check if there are results.

PHP Code:

if(SQL_NumResults(Query) < 1)
{
    
//no results found -> you can insert now
    
Query SQL_PrepareQuery(SqlConnection"INSERT INTO `mostplay_players`(`nick`,`protocol`,`ip`,`active`) VALUES ('%s','%s','%s','1')"nameprotocoluserip);
}
else
{
    
//there are results -> you have to replace
    
Query SQL_PrepareQuery(SqlConnection"UPDATE  `mostplay_players` SET `nick` = '%s', `protocol` = '%s', `ip` = '%s', `active` = '1' WHERE `mostplay_players`.`protocol` = '%s'"nameprotocoluseripprotocol)



issen1 10-19-2010 03:45

Re: Function Help
 
Do _not_ perform two queries. Use the following:
http://dev.mysql.com/doc/refman/5.1/...duplicate.html

For an in-depth example look at the provided link.

Edit: Also, you do not need to repeat the SET statement all the time, Schwabba. Look here: http://dev.mysql.com/doc/refman/5.1/en/update.html

Edit2: If you have a where-clause restricting the query to a specific protocol, you don't need to update these, since it has no effect.

Schwabba 10-19-2010 05:16

Re: Function Help
 
Quote:

Originally Posted by issen1 (Post 1329101)
Also, you do not need to repeat the SET statement all the time, Schwabba.

Right, copied wrong.

IchWill 10-21-2010 18:35

Re: Function Help
 
Quote:

Originally Posted by issen1 (Post 1329101)
Do _not_ perform two queries. Use the following:
http://dev.mysql.com/doc/refman/5.1/...duplicate.html

For an in-depth example look at the provided link.

Edit: Also, you do not need to repeat the SET statement all the time, Schwabba. Look here: http://dev.mysql.com/doc/refman/5.1/en/update.html

Edit2: If you have a where-clause restricting the query to a specific protocol, you don't need to update these, since it has no effect.

Thanx issen1 that works :crab: .


All times are GMT -4. The time now is 10:22.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.