Raised This Month: $51 Target: $400
 12% 

Save player data to mysql


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jonatat
Senior Member
Join Date: Dec 2017
Old 05-31-2018 , 02:25   Save player data to mysql
Reply With Quote #1

Hello all. I have simple SQL request from server to save player data into mysql. Like kills, deaths, steam_id and etc. Then player join in the server, made some kills SQL not update existing table row, but creating a new one without steam id, nickname and etc... Whats wrong? My code:

PHP Code:
public SavePlayer(id)
{
if(!
pLoaded[id]) 
return 
PLUGIN_HANDLED;

if(
pPoints[id] < 0)
pPoints[id] = 0;

new 
Name[64], steam[64];
get_user_name(idName63);

get_user_authid(idsteam63);

static 
szQuery[3800];
formatexszQuery3799"REPLACE INTO `users` (`steam_id`, `player_name`, `kills`, `deaths`, `headshots`, `aces`, `m_aces`, `mix_played`, `mix_lost`, `mix_won`, `mix_draw`, `points`, `last_online`) VALUES ('%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', NOW());"steamGetSecureName(Name), pKills[id], pDeaths[id], pHeadshots[id], pAces[id], pMiniAces[id], pMixPlayed[id], pMixLost[id], pMixWon[id], pMixDraw[id], pPoints[id]);
#if AMXX_VERSION_NUM >= 183
SQL_SetCharset(g_hTuple,"utf8");
#endif

SQL_ThreadQuery(g_hTuple"QuerySetData"szQuery);

return 
PLUGIN_CONTINUE;

jonatat is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 05-31-2018 , 11:07   Re: Save player data to mysql
Reply With Quote #2

Use update instead of replace.
https://www.w3schools.com/sql/sql_update.asp

Also, you do not need to specify all columns if you set values in correct order, and format with %i for integers instead of %d.

Code:
INSERT INTO 'users' VALUES ('%s', '%s', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', NOW());", steam, GetSecureName(Name), pKills[id], pDeaths[id], pHeadshots[id], pAces[id], pMiniAces[id], pMixPlayed[id], pMixLost[id], pMixWon[id], pMixDraw[id], pPoints[id]);

More info about sql: https://forums.alliedmods.net/showth...p?t=172936#SQL
__________________









Last edited by CrazY.; 05-31-2018 at 11:16.
CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-31-2018 , 17:52   Re: Save player data to mysql
Reply With Quote #3

CrazY your query is wrong. You are not specifying the fields for the data you are trying to set.

My bad crazy, you're right. As long as all fields are supplied and in the correct order, your method will work. I've never personally used INSERT without specifying field names and I literally write SQL at work almost every day. Still, I think it's good practice to include field names, but thats just my opinion.

jonatat, when a player connects then use a query to retrieve their data and store it in an array. On disconnect, if data exists in the array, use an UPDATE statement. If the array is null, use an INSERT statement.

Code:
INSERT INTO tblTest (SteamID, Val1, Val2) VALUES ('STEAM:0:12345',12,34);

UPDATE tblTest SET Val1=55, Val2=66 WHERE SteamID='STEAM:0:12345';
__________________

Last edited by Bugsy; 05-31-2018 at 20:14.
Bugsy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-31-2018 , 18:06   Re: Save player data to mysql
Reply With Quote #4

You can also use REPLACE INTO if your table is created properly.

Code:
REPLACE works exactly like INSERT , except that if an old row in the table has 
the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is 
deleted before the new row is inserted. See Section 12.2.5, “ INSERT Syntax”. 
REPLACE is a MySQL extension to the SQL standard.
You can make the steam ID your primary key since this value is static.
Code:
CREATE TABLE IF NOT EXISTS tblPlayerData (SteamID VARCHAR(34) PRIMARY KEY , PlayerName VARCHAR(32), PlayTime INTEGER);

REPLACE INTO tblPlayerData (SteamID, PlayerName, PlayTime) VALUES ('STEAM123','bugsy',1);
__________________

Last edited by Bugsy; 05-31-2018 at 18:09.
Bugsy is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 05-31-2018 , 20:29   Re: Save player data to mysql
Reply With Quote #5

@Bugsy, yes, this is a prefer of each programmer, both will work. I did not found results about REPLACE INTO in w3c, only about replace strings. Are you sure that this will work?
__________________








CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-31-2018 , 20:33   Re: Save player data to mysql
Reply With Quote #6

Quote:
Originally Posted by CrazY. View Post
@Bugsy, yes, this is a prefer of each programmer, both will work. I did not found results about REPLACE INTO in w3c, only about replace strings. Are you sure that this will work?
I'm not sure what you mean but yes, what I posted works and I tested it. The key thing (no pun intended) is defining the primary key and then as long as that value already exists in a row, the other fields will be updated. In my example, steam id.
__________________

Last edited by Bugsy; 05-31-2018 at 20:34.
Bugsy is offline
jonatat
Senior Member
Join Date: Dec 2017
Old 06-01-2018 , 04:01   Re: Save player data to mysql
Reply With Quote #7

Thanks guys for helping me out! Can i use UPDATE instead of REPLACE? Becouse i'm using same users table in server and website
jonatat is offline
jonatat
Senior Member
Join Date: Dec 2017
Old 06-01-2018 , 04:14   Re: Save player data to mysql
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
You can also use REPLACE INTO if your table is created properly.

Code:
REPLACE works exactly like INSERT , except that if an old row in the table has 
the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is 
deleted before the new row is inserted. See Section 12.2.5, “ INSERT Syntax”. 
REPLACE is a MySQL extension to the SQL standard.
You can make the steam ID your primary key since this value is static.
Code:
CREATE TABLE IF NOT EXISTS tblPlayerData (SteamID VARCHAR(34) PRIMARY KEY , PlayerName VARCHAR(32), PlayTime INTEGER);

REPLACE INTO tblPlayerData (SteamID, PlayerName, PlayTime) VALUES ('STEAM123','bugsy',1);
You really helped me with PRIMARY_KEY!

Does it okay?

Code:
formatex( szQuery, 3799, "UPDATE `users` SET `steam_id` = '%s', `player_name` = '%s', `kills` = '%d', `deaths` = '%d', `headshots` = '%d', `aces` = '%d', `m_aces` = '%d', `mix_played` = '%d', `mix_lost` = '%d', `mix_won` = '%d', `mix_draw` = '%d', `points` = '%d' WHERE `steam_id` = '%s';", steam, GetSecureName(Name), pKills[id], pDeaths[id], pHeadshots[id], pAces[id], pMiniAces[id], pMixPlayed[id], pMixLost[id], pMixWon[id], pMixDraw[id], pPoints[id], steam);

Last edited by jonatat; 06-01-2018 at 04:16.
jonatat is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-01-2018 , 10:08   Re: Save player data to mysql
Reply With Quote #9

Yes, you can. Just do the syntax correctly and will work.

Quote:
Code:
formatex( szQuery, 3799, "UPDATE `users` SET `steam_id` = '%s', `player_name` = '%s', `kills` = '%d', `deaths` = '%d', `headshots` = '%d', `aces` = '%d', `m_aces` = '%d', `mix_played` = '%d', `mix_lost` = '%d', `mix_won` = '%d', `mix_draw` = '%d', `points` = '%d' WHERE `steam_id` = '%s';", steam, GetSecureName(Name), pKills[id], pDeaths[id], pHeadshots[id], pAces[id], pMiniAces[id], pMixPlayed[id], pMixLost[id], pMixWon[id], pMixDraw[id], pPoints[id], steam);
Looks like it's all fine, test it.
__________________








CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-01-2018 , 13:52   Re: Save player data to mysql
Reply With Quote #10

You don't need to update the steamid value
__________________
Bugsy is offline
Old 06-08-2018, 09:01
4ever16
This message has been deleted by 4ever16.
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 05:10.


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