MYSQL register/login system problem
#1

I tried to mimic xSladeHDx's mysql register/login system, but I failed completely. My system doesn't even show the dialog now, and it didn't use to register the account. I have the full code here:
Код:
public OnPlayerConnect(playerid)	
{
	new query[126], pName[MAX_PLAYER_NAME];
	GetPlayerName(playerid, pName, sizeof(pName));
	
	format(query, sizeof(query), "SELECT * FROM accounts WHERE name = '%s'", pName);
	mysql_query(query);
	mysql_store_result();
	
	if(mysql_num_rows() == 1)
	{
		SCM(pid, -1, "That username is registered!");
		ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login", "Please login with your password.", "Login", "Cancel");
	}
	else if(mysql_num_rows() == 0)
	{	
		SCM(pid, -1, "That username is not registered. You may use it.");
		ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Register", "Please enter a password to register.", "Register", "Cancel");
	}	 
	return 1;
}


stock SavePlayer(playerid)
{
	new query[126], pName[MAX_PLAYER_NAME];
	GetPlayerName(playerid, pName, sizeof(pName));
	
	GetPlayerPos(playerid, Player[playerid][Position][0], Player[playerid][Position][1], Player[playerid][Position][2]);
	
	format(query, sizeof(query), "UPDATE accounts SET Name = '%s', Password = '%s', PlayerX = '%f', PlayerY = '%f', PlayerZ = '%f', pAdmin = '%d', pHelper = '%d', pVip = '%d' WHERE Name = '%s'", pName, Player[playerid][Password], Player[playerid][Position][0], Player[playerid][Position][1], Player[playerid][Position][2], Player[playerid][pAdmin], Player[playerid][pHelper],Player[playerid][pVip], pName);
	mysql_query(query);
	printf("Player ID %d (%s) has been saved.", playerid, pName);
}

public OnPlayerDisconnect(playerid, reason)
{
	SavePlayer(playerid);
	return 1;
}

stock GetName(playerid)
{
	new playerName[MAX_PLAYER_NAME];
	
	GetPlayerName(playerid, playerName, sizeof(playerName));
	
	return playerName;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	switch(dialogid)
	{
		case 0:
		{
			if(response)
			{
				new query[126], pName[MAX_PLAYER_NAME];
				
				GetPlayerName(playerid, pName, sizeof(pName));
				format(query, sizeof(query), "SELECT * FROM accounts WHERE name = '%s' AND password = '%s'", pName, inputtext);
				mysql_query(query);
				mysql_store_result();
				
				if(mysql_num_rows() != 0)
				{
					SCM(pid, -1, "Logging in.");
					LoginPlayer(playerid);
				}
				else
				{
					SCM(pid, -1, "Invalid password!");
					ShowPlayerDialog(playerid, 0, DIALOG_STYLE_INPUT, "Login", "Please login with your password", "Login", "Cancel");
				}
			}
		}
		case 1:
		{
			if(response)
			{
				new query[126], pName[MAX_PLAYER_NAME];
				GetPlayerName(playerid, pName, sizeof(pName));
				
				format(query, sizeof(query), "INSERT INTO accounts VALUES ('%s', '%s')", pName, inputtext);
				mysql_query(query);
				
				SCM(pid, -1, "You have registered successfully!");
				SpawnPlayer(playerid);
			}
		}
	}
	return 1;
}

stock RegisterPlayer(playerid, regpass[])
{
	new query[350], EncryptedPass[130];
	
	WP_Hash(EncryptedPass, sizeof(EncryptedPass), regpass);
	
	format(query, sizeof(query), "INSERT INTO accounts (Name, Password, pAdmin, pHelper, pLeader) VALUES ('%s', '%s', '%d', '%d', '%d')", GetName(playerid), EncryptedPass);
	mysql_query(query);
	
	SavePlayer(playerid);
	
	return 1;
}
stock LoginPlayer(playerid)
{
	new query[126], pName[MAX_PLAYER_NAME];
	
	GetPlayerName(playerid, pName, sizeof(pName));
	format(query, sizeof(query), "SELECT * FROM accounts WHERE Name = '%s'", pName);
	mysql_query(query);
	mysql_store_result();
	
	while(mysql_fetch_row_format(query, "|"))
	{
		mysql_fetch_field_row(Player[playerid][Name], "Name");
		mysql_fetch_field_row(Player[playerid][Password], "Password");	

		mysql_fetch_field_row(query, "PlayerX"); Player[playerid][Position][0] = floatstr(query);
		mysql_fetch_field_row(query, "PlayerY"); Player[playerid][Position][1] = floatstr(query);
		mysql_fetch_field_row(query, "PlayerZ"); Player[playerid][Position][2] = floatstr(query);
		mysql_fetch_field_row(query, "pAdmin"); Player[playerid][pAdmin] = strval(query);
		mysql_fetch_field_row(query, "pHelper"); Player[playerid][pHelper] = strval(query);
		mysql_fetch_field_row(query, "pVip"); Player[playerid][pVip] = strval(query);
		
	}
	mysql_free_result();
	SetSpawnInfo(playerid, 0, 294, Player[playerid][Position][0], Player[playerid][Position][1], Player[playerid][Position][2], 0, 0, 0, 0, 0, 0, 0);
	SpawnPlayer(playerid);
}
Reply
#2

Which MySQL plugin are you using ?
BlueG's or GStyle'z ?
Reply
#3

I don't remember. Which one would you recommend?
edit: I just installed blue's mysql and I got errors for lines like
Код:
 mysql_fetch_field_row
Reply
#4

BlueG's recommended
Alright I will fix up the first phase for you:
Note that I used // where I had to fix something
PHP код:
public OnPlayerConnect(playerid)    
{
    new 
query[126], pName[MAX_PLAYER_NAME], Cache:result// added a cached variable instead of a threaded query, threaded queries are faster for me but you need to start from the basic
    
GetPlayerName(playeridpNamesizeof(pName));
    
    
mysql_format(mysqlquerysizeof(query), "SELECT `admin` FROM `accounts` WHERE `name` = '%e'"pName); // Added `` between table name and column name
// The chosen column is Admin, use any column that exist in your table just to verify
//%e sepcifier to ignore escape string, You also have to turn it to mysql_format and add the connection handler to use the %e specifier
    
result mysql_query(mysqlquery); // You must identify the connection handle, store the cache in the variable
    
    
if(cache_get_row_count()) // if there were any rows found
    
{
        
SCM(pid, -1"That username is registered!");
        
ShowPlayerDialog(playerid1DIALOG_STYLE_INPUT"Login""Please login with your password.""Login""Cancel");
    }
    else 
// No rows were found
    
{    
        
SCM(pid, -1"That username is not registered. You may use it.");
        
ShowPlayerDialog(playerid2DIALOG_STYLE_INPUT"Register""Please enter a password to register.""Register""Cancel");
    }     
    return 
1;

About LoginPlayer,
Select all from the table and store it in a Cached variable
and then simply use cache_get_field_content for strings, cache_get_field_content_int for integers and cache_get_field_content_float for floats

See more here:
https://sampwiki.blast.hk/wiki/MySQL/R33#Cache_functions

Note: You HAVE to use cache_delete(cache id); after EVERY cache usage, unless you spend memory for nothing.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)