Raised This Month: $ Target: $400
 0% 

Function Help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
IchWill
Junior Member
Join Date: Sep 2010
Old 10-18-2010 , 16:22   Function Help
Reply With Quote #1

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)
IchWill is offline
Schwabba
Senior Member
Join Date: Apr 2008
Old 10-19-2010 , 03:23   Re: Function Help
Reply With Quote #2

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)


Last edited by Schwabba; 10-19-2010 at 05:17.
Schwabba is offline
issen1
Member
Join Date: Jan 2010
Old 10-19-2010 , 03:45   Re: Function Help
Reply With Quote #3

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.
__________________
greets (:

Last edited by issen1; 10-19-2010 at 03:49.
issen1 is offline
Schwabba
Senior Member
Join Date: Apr 2008
Old 10-19-2010 , 05:16   Re: Function Help
Reply With Quote #4

Quote:
Originally Posted by issen1 View Post
Also, you do not need to repeat the SET statement all the time, Schwabba.
Right, copied wrong.
Schwabba is offline
IchWill
Junior Member
Join Date: Sep 2010
Old 10-21-2010 , 18:35   Re: Function Help
Reply With Quote #5

Quote:
Originally Posted by issen1 View Post
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 .
IchWill is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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