25.01.2015, 08:12
Hey guys, Iґve got 2 small Problems with MySQL
Iґm saving the players spawn positions and it should be set to them after a player relogs but it always sets my position to the same spot even when the positions are getting saved
And the 2nd Problem is that when I create another account it saves the Stats on the same row where the first player is saved
Codes:
Iґm saving the players spawn positions and it should be set to them after a player relogs but it always sets my position to the same spot even when the positions are getting saved
And the 2nd Problem is that when I create another account it saves the Stats on the same row where the first player is saved
Codes:
PHP код:
public OnPlayerConnect(playerid)
{
SetPlayerColor(playerid, 0xBEBEBEFF);
new query[1024];
GetPlayerName(playerid, Name[playerid], 24);
GetPlayerIp(playerid, IP[playerid], 16);
mysql_format(mysql, query, sizeof(query),"SELECT `Password`, `ID` FROM `accounts` WHERE `Name` = '%e' LIMIT 1", Name[playerid]);
// - We use mysql_format instead of format because we can use an %e specifier. %e specifier escapes a string so we can avoid sql injection which means we don't have to use mysql_real_escape_string
// - Formatting our query; SELECT `Password`, `ID` FROM `players` WHERE `Username`='%e' means we are selecting a Password and ID's column in the table that has player's name in Username column.
// - LIMIT 1; we only need 1 result to be shown
mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
//lets execute the formatted query and when the execution is done, a callback OnAccountCheck will be called
//You can name the callback however you like
return 1;
}
PHP код:
forward OnAccountCheck(playerid);
public OnAccountCheck(playerid)
{
new rows, fields; //a variable that will be used to retrieve rows and fields in the database.
cache_get_data(rows, fields, mysql);//let's get the rows and fields from the database.
if(rows)
{
cache_get_field_content(0, "Password", pInfo[playerid][Password], mysql, 129);
//we will load player's password into pInfo[playerid][Password] to be used in logging in
pInfo[playerid][ID] = cache_get_field_content_int(0, "ID"); //now let's load player's ID into pInfo[playerid][ID] so we can use it later
//printf("%s", pInfo[playerid][Password]); //OPTIONAL: Just for debugging. If it didn't show your password, then there must be something wrong while getting player's password
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "In order to play, you need to login", "Login", "Quit"); //And since we found a result from the database, which means, there is an account; we will show a login dialog
}
else //if we didn't find any rows from the database, that means, no accounts were found
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "In order to play, you need to register.", "Register", "Quit");
//So we show them a dialog register
}
return 1;
}
PHP код:
public OnAccountRegister(playerid)
{
pInfo[playerid][ID] = cache_insert_id(); //loads the ID of the player in the variable once they registered.
printf("New account registered. ID: %d", pInfo[playerid][ID]); //just for debugging.
return 1;
}
PHP код:
public OnPlayerDisconnect(playerid, reason)
{
new Query[256];
GetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ]);
mysql_format(mysql, Query, sizeof(Query), "UPDATE accounts SET Score='%d', VIP='%d', Money='%d', PosX='%.6f', PosY='%.6f', PosZ='%.6f' WHERE ID='%d'",
pInfo[playerid][Score], pInfo[playerid][VIP], pInfo[playerid][Money], pInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ], pInfo[playerid][ID]);
printf("%s", Query);
mysql_tquery(mysql, Query, "", "");
return 1;
}
PHP код:
public OnPlayerSpawn(playerid)
{
SetPlayerMapIcon( playerid, 1, 701.8041,-519.4351,16.3318, 55, 0, MAPICON_LOCAL );
SetPlayerMapIcon( playerid, 2, 661.3626,-573.4266,16.3359, 52, 0, MAPICON_LOCAL );
SetPlayerColor(playerid, 0xFFFFFFFF);
SetPlayerInterior(playerid,0);
SetPlayerVirtualWorld(playerid,0);
SetTimerEx("SetPos", 600, false, "i", playerid);
SetPlayerSkin(playerid, pInfo[playerid][pSkin]);
SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
return 1;
}
PHP код:
forward SetPos(playerid);
public SetPos(playerid)
{
SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
return 1;
}