GetPlayerWeaponData and MySql
#1

Hey, how would you save
PHP Code:
 
new weapons[13][2];
 
for (new 
0<= 12i++)
{
    
GetPlayerWeaponData(playeridiweapons[i][0], weapons[i][1]);

In players table without creating a row for every weapon?
Reply
#2

Not in the `players` table! Make a new table for the weapons and follow this tutorial: https://sampforum.blast.hk/showthread.php?tid=505081
However add a new column called `slot` and set as UNIQUE KEY the columns (userid, slot) so it will overwrite the previous weapon in the same slot.
Reply
#3

Quote:
Originally Posted by Calisthenics
View Post
Not in the `players` table! Make a new table for the weapons and follow this tutorial: https://sampforum.blast.hk/showthread.php?tid=505081
However add a new column called `slot` and set as UNIQUE KEY the columns (userid, slot) so it will overwrite the previous weapon in the same slot.
He better create columns and use loop to save, e.g weapon1, weapon2.
Reply
#4

Quote:
Originally Posted by ConnorW
View Post
He better create columns and use loop to save, e.g weapon1, weapon2.
Why 'better'? Say a player is unarmed, is it the same 12 columns with values of 0 than 0 rows? I do not think so.
Reply
#5

Quote:
Originally Posted by Calisthenics
View Post
Why 'better'? Say a player is unarmed, is it the same 12 columns with values of 0 than 0 rows? I do not think so.
You simply do a variable for that, and save each weapon. Way much better then a totally new table just for weapons.
Reply
#6

https://sampforum.blast.hk/showthread.php?tid=505081
Reply
#7

Quote:
Originally Posted by ConnorW
View Post
You simply do a variable for that, and save each weapon. Way much better then a totally new table just for weapons.
Using a variable does not prevent you from having 12 "empty" columns. You still define 'better' as what is your preference but not what is right.

If you think a new table "just" for weapons, you could say the same for anything else and end up with your main table having hundreds of columns. A good thumb rule is to keep a table to 10-20 columns max.

https://en.wikipedia.org/wiki/First_..._form#Examples

Violation of 1NF is seen widely on this forum by using columnX or a column separated with multiple values such "0|0|0|1|0|0|0|1".

Another one is storing dates as VARCHAR instead of TIMESTAMP/DATETIME which does not allow you to manipulate the data, get the difference between two dates and what not.
Reply
#8

Quote:
Originally Posted by Y_Less
View Post
This is the exact opposite of good database schema design. If you ever have a column with a numeric suffix you are doing things wrong. A new table just for weapons is absolutely the best way - tables don't have to be (indeed, shouldn't be) monolithic.
Code:
new playerweapons[MAX_PLAYERS][12];

SaveWeapons(playerid)
{
	new playername[MAX_PLAYER_NAME], string[164];
	GetPlayerName(playerid, playername, sizeof(playername));
 
	for (new i = 0; i <= 12; i++)
	{
		mysql_format(dbconnection, string, sizeof(string), "UPDATE `usertable` SET `weapon%d` = %d WHERE `name` = '%s'", i, playerweapons[playerid][i], playername);
		mysql_tquery(dbconnection, string);
	}
}
Wouldn't this be the same?
Reply
#9

Quote:
Originally Posted by ConnorW
View Post
Wouldn't this be the same?
Just create a new table.
Reply
#10

Quote:
Originally Posted by jasperschellekens
View Post
Just create a new table.
I know you can just create a table for that, but you'll load the table, so instead loading two tables, users and weapons, you can just load one, you get me?
I wasn't me, the one who did requested this help, but i just told him the way i would have saved the player weapons.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)