SA-MP Forums Archive
MySQL - Floats + AccountID - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySQL - Floats + AccountID (/showthread.php?tid=559773)



MySQL - Floats + AccountID - Saize - 25.01.2015

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:

PHP код:
public OnPlayerConnect(playerid)
{
    
SetPlayerColor(playerid0xBEBEBEFF);
    new 
query[1024];
    
GetPlayerName(playeridName[playerid], 24);
    
GetPlayerIp(playeridIP[playerid], 16);
    
mysql_format(mysqlquerysizeof(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(mysqlquery"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 
rowsfields//a variable that will be used to retrieve rows and fields in the database.
    
cache_get_data(rowsfieldsmysql);//let's get the rows and fields from the database.
    
if(rows)
    {
        
cache_get_field_content(0"Password"pInfo[playerid][Password], mysql129);
        
//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(playeridDIALOG_LOGINDIALOG_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(playeridDIALOG_REGISTERDIALOG_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(playeridreason)
{
    new 
Query[256];
    
GetPlayerPos(playeridpInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ]);
    
mysql_format(mysqlQuerysizeof(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(mysqlQuery"""");
    return 
1;

PHP код:
public OnPlayerSpawn(playerid)
{
    
SetPlayerMapIconplayerid1701.8041,-519.4351,16.3318550MAPICON_LOCAL );
    
SetPlayerMapIconplayerid2661.3626,-573.4266,16.3359520MAPICON_LOCAL );
    
SetPlayerColor(playerid0xFFFFFFFF);
    
SetPlayerInterior(playerid,0);
    
SetPlayerVirtualWorld(playerid,0);
    
SetTimerEx("SetPos"600false"i"playerid);
    
    
SetPlayerSkin(playeridpInfo[playerid][pSkin]);
    
SetPlayerPos(playeridpInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
    return 
1;

PHP код:
forward SetPos(playerid);
public 
SetPos(playerid)
{
    
SetPlayerPos(playeridpInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
    return 
1;




Re: MySQL - Floats + AccountID - Ironboy - 25.01.2015

Do you get any errors on the logs?


AW: Re: MySQL - Floats + AccountID - Saize - 25.01.2015

Quote:
Originally Posted by Ironboy
Посмотреть сообщение
Do you get any errors on the logs?
Nop its all fine atleast while compiling


Re: MySQL - Floats + AccountID - Vince - 25.01.2015

OnPlayerDisconnect is unreliable for many things. This callback gets called after the player has already disconnected so you can't get his position anymore.


AW: Re: MySQL - Floats + AccountID - Saize - 25.01.2015

Quote:
Originally Posted by Vince
Посмотреть сообщение
OnPlayerDisconnect is unreliable for many things. This callback gets called after the player has already disconnected so you can't get his position anymore.
Iґve already fixxed the problem with saving/reading the floats but it rounds up/down itself and thats kinda f***ed up


The other problem is that I cant register a new account, it usually should create a new row with the next higher ID but it just doesnt

How can I fix this 2 problems?


AW: Re: MySQL - Floats + AccountID - Saize - 25.01.2015

Quote:
Originally Posted by Saize
Посмотреть сообщение
Iґve already fixxed the problem with saving/reading the floats but it rounds up/down itself and thats kinda f***ed up


The other problem is that I cant register a new account, it usually should create a new row with the next higher ID but it just doesnt

How can I fix this 2 problems?
keep this up please


Re: MySQL - Floats + AccountID - Ironboy - 25.01.2015

The above code doesn't have any problems, show your register dialog.


AW: Re: MySQL - Floats + AccountID - Saize - 26.01.2015

Quote:
Originally Posted by Ironboy
Посмотреть сообщение
The above code doesn't have any problems, show your register dialog.
Here we go

PHP код:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_LOGIN//login dialog
        
{
            if(!
responseKick(playerid); //if they clicked Quit, we will kick them
            
new hpass[129]; //for password hashing
              
new query[1024]; // for formatting our query.
            
WP_Hash(hpass129inputtext); //hashing inputtext
            
if(!strcmp(hpasspInfo[playerid][Password])) //remember we have loaded player's password into this variable, pInfo[playerid][Password] earlier. Now let's use it to compare the hashed password with password that we load
            
//if the hashed password matches with the loaded password from database
                
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1"Name[playerid]);
                
//let's format our query
                //We select all rows in the table that has your name and limit the result to 1
                
mysql_tquery(mysqlquery"OnAccountLoad""i"playerid);
                
//lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
                //You can name the callback however you like
            
}
            else 
//if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
            
{
                
//we tell them that they have inserted a wrong password
                
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"Login""In order to play, you need to login\nWrong password!""Login""Quit");
            }
        }
        case 
DIALOG_REGISTER//register dialog
        
{
            if(!
response) return Kick(playerid); //if they clicked Quit, we will kick them
            
if(strlen(inputtext) < 6) return ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"Register""In order to play, you need to register.\nYour password must be at least 6 characters long!""Register""Quit");
            
//strlen checks a lenght of a string. so if player types their password that is lower than 6, we tell them; Your password must be at least 6 characters long!
            
new query[1024];
            
WP_Hash(pInfo[playerid][Password], 129inputtext); //hashing inputtext
            
mysql_format(mysqlquerysizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `PosX`, `PosY`, `PosZ`, `FacingAngel`) VALUES ('%e', '%s', 701.8041,-519.4351,16.3318,261.1537)"Name[playerid], pInfo[playerid][Password]);
            
//Now let's create a new row and insert player's information in it
            
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid);
            
//let's execute the query
        
}
    }
    return 
1;




AW: MySQL - Floats + AccountID - Saize - 26.01.2015

keep this up please


AW: Re: MySQL - Floats + AccountID - Saize - 31.01.2015

Quote:
Originally Posted by Saize
Посмотреть сообщение
Here we go

PHP код:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_LOGIN//login dialog
        
{
            if(!
responseKick(playerid); //if they clicked Quit, we will kick them
            
new hpass[129]; //for password hashing
              
new query[1024]; // for formatting our query.
            
WP_Hash(hpass129inputtext); //hashing inputtext
            
if(!strcmp(hpasspInfo[playerid][Password])) //remember we have loaded player's password into this variable, pInfo[playerid][Password] earlier. Now let's use it to compare the hashed password with password that we load
            
//if the hashed password matches with the loaded password from database
                
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1"Name[playerid]);
                
//let's format our query
                //We select all rows in the table that has your name and limit the result to 1
                
mysql_tquery(mysqlquery"OnAccountLoad""i"playerid);
                
//lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
                //You can name the callback however you like
            
}
            else 
//if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
            
{
                
//we tell them that they have inserted a wrong password
                
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"Login""In order to play, you need to login\nWrong password!""Login""Quit");
            }
        }
        case 
DIALOG_REGISTER//register dialog
        
{
            if(!
response) return Kick(playerid); //if they clicked Quit, we will kick them
            
if(strlen(inputtext) < 6) return ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"Register""In order to play, you need to register.\nYour password must be at least 6 characters long!""Register""Quit");
            
//strlen checks a lenght of a string. so if player types their password that is lower than 6, we tell them; Your password must be at least 6 characters long!
            
new query[1024];
            
WP_Hash(pInfo[playerid][Password], 129inputtext); //hashing inputtext
            
mysql_format(mysqlquerysizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `PosX`, `PosY`, `PosZ`, `FacingAngel`) VALUES ('%e', '%s', 701.8041,-519.4351,16.3318,261.1537)"Name[playerid], pInfo[playerid][Password]);
            
//Now let's create a new row and insert player's information in it
            
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid);
            
//let's execute the query
        
}
    }
    return 
1;

Push