Help with MYSQL, Avoid Dual Records?
#1

Hey Guys... Me Again xD... Im triying to learn MYSQL.... Im using G-sTyLeZzZ Plugin...
im having issues with a register command im triying to make... i have this....

Код:
CMD:register(playerid,params[])
{
	new query[256];
	if(isnull(params)) return SendClientMessage(playerid,COLOR_SERVER_WARNING,"[USAGE]: /REGISTER [PASSWORD]");
	if(strlen(params) < 6) return SendClientMessage(playerid,COLOR_SERVER_WARNING,"[ERROR]: Password should contain at least 6 Characters!");
	mysql_real_escape_string(params,params);
	format(query,sizeof(query),"INSERT INTO `%s` (Username,Password) VALUES ('%s',md5('%s'))",SQL_TABLE,PName(playerid),params);
	mysql_query(query);
	if(!IsPlayerConnected(playerid)) return AvoidSync();
	mysql_store_result();
	if(mysql_affected_rows())
	{
		SendClientMessage(playerid,COLOR_SERVER,"Congratulations, You'r account is now registered!, Please Log In!");
		PA[playerid][Registered] = true;
	}
	else
	{
	    SendClientMessage(playerid,COLOR_SERVER_WARNING,"[ERROR]: Registration Failed!");
	}
	return 1;
}
The thing is... I was wondering.... Is there a way to avoid dual registry?... So people with the same name cant register?.... I have tried to register... after restarting the GM... an it creates the Player, but it increases the ID so...
Reply
#2

pawn Код:
new name[24];
mysql_escape_string(Pname(playerid), name);
format(query,sizeof(query),"SELECT * FROM `%s`  WHERE Username = %s",SQL_TABLE, name);
mysql_query(query);
mysql_store_result();
if(mysql_num_rows > 0) return SendClientMessage(playerid,COLOR_SERVER_WARNING,"[ERROR]: This username is already registered!");
mysql_free_result();
That should do it .
Reply
#3

Thanks... its working.... it seems... i saw the tutorial on you sig... gonna use it.... :P... if you dont mind me asking these, there are a few funtions i dont really get... like... the "mysql_real_escape_string"... What is it for?... or whats the result stored on Mysql_store_result?... also... The part were you do the mysql_num_rows... it checks if in the database, from the query, has been found rows?
Reply
#4

Quote:
Originally Posted by almighty
Посмотреть сообщение
Thanks... its working.... it seems... i saw the tutorial on you sig... gonna use it.... :P... if you dont mind me asking these, there are a few funtions i dont really get... like... the "mysql_real_escape_string"... What is it for?... or whats the result stored on Mysql_store_result?... also... The part were you do the mysql_num_rows... it checks if in the database, from the query, has been found rows?
MySQL_Escape_Query makes it so that players cannot SQL inject because if they do it could delete your whole table.

MySQL_Store_Result is just storing the information that you are trying to retrieve from the database.

MySQL_Num_Rows is checking how much rows the stored result returned. I made it more than 0 because if the mysql connection is down it's -1 and if there is no existing rows then it is 0.

For more information visit https://sampwiki.blast.hk/wiki/MySQL
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)