[Tutorial] Mysql Register/Login system!
#1

Hello all, so I'l be explaining how to make a simple/basic register system, MySQL based.
This will be just a start of, with money and score, then you can edit more you're self.

What will I need?
For this tutorial, we will be using BlueG mysql plugin,and sscanf.
[REL] MySQL Plugin R6-2 https://sampforum.blast.hk/showthread.php?tid=56564
sscanf 2.0 - Fixed for 0.3d https://sampforum.blast.hk/showthread.php?tid=120356
After you installed it all continue :b

Creating the table
So connect to your phpmyadmin, or what ever your using,
And it should look something like this:

Then click on your database, and create a new table with 3 rows (as i said, im just explaining with the money)
1 For the Username, 1for the Password and 1 for the Money
Now it should look like this:

Sorry mine is in portuese :/
So where it says field you will put Money
And change the type to Int then press save.
Now errors, or mistakes? Great! Lets continue

Scripting
So on the top of your script you should add
#include <a_mysql>

So now any where in your script except functions or stocks
(I recomend, on top of OnGameModeInit) add:
This is much better and organized, to know your database info.
pawn Код:
#define mysql_host "YourHostIp"
#define mysql_user "YourMysqlUser"
#define mysql_password "YourUserPassword"
#define mysql_database "YourDatabaseName"
And in GameModeInit add
pawn Код:
public OnGameModeInit()
{
mysql_connect(mysql_host,mysql_user,mysql_database ,mysql_password);
return 1;
}
What will this do?
So it will connect your server to the database, if it doesn't try adding still OnGameModeInit
mysql_debug(1);
And check Debug.txt in your server directory.

Now lets check if the player is registered
Add this under OnPlayerConnect, as so:

pawn Код:
public OnPlayerConnect(playerid)
{
new Query[80],pName[24],string[164];
GetPlayerName(playerid,pName,24);
format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
mysql_query(Query);
mysql_store_result();
return 1;
}
This will select and store the result, retrieved from your table.
Without this, it wouldn't check if people were registered or not.
pawn Код:
public OnPlayerConnect(playerid)
{
new Query[80],pName[24],string[164];
GetPlayerName(playerid,pName,24);
format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
mysql_query(Query);
mysql_store_result();
if(mysql_num_rows() != 0)//if number of rows is different from 0 then continue
{
format(string,sizeof(string),"Hey, %s! \nYour account is registered.\nPlease enter the password to log in!",pName);
ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Lo g in",string,"Login","");
}
else
{
format(string,sizeof(string),"Hey, %s! \nYour account is not registered. \nPlease register to continue!",pName);
ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Re gister",string,"Register","");
}
mysql_free_result();
return 1;
}
So there is checking if he's registered or not, if not then show the dialog of register, else show the login dialog.
Did you notice i added mysql_free_result();?
Why? Because we used mysql_store_result() so lets free it for less memory usage.
Then we are using mysql_num_rows() != 0 to see if there are more than 0 rows.
If so it means he is registered
Lets check the register dialog code:
pawn Код:
if(dialogid == 1)
{
if(strlen(inputtext) == 0)
{
ShowPlayerDialog(playerid,Regdialog,DIALOG_STYLE_I NPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password"cblue" for it! \n","Register!","");
}
else
{
new EscapedText[60];
mysql_real_escape_string(inputtext, EscapedText);
format(Query,sizeof(Query),"INSERT INTO `Users` (Username,Password,Money) VALUES ('%s','%s,'0')",GetPName(playerid),EscapedText);
mysql_query(Query);
SendClientMessage(playerid,green,"You have been successfully registered!");
GivePlayerMoney(playerid,5000);
SetPlayerScore(playerid,100);
}
}
Why are we using:
mysql_real_escape_string(inputtext, EscapedText);?
So for example he can't use his password as DROP `Users`
That would delete your table Users. So with that function even if he uses that it won't delete your table.
After we are using INSERT INTO `Tablename` So in this case, it will insert the Username the password, and the money, then we are using VALUES, so it will insert in those rows, the selected values.
So in this case it will insert Username, his password, and his money
Lets try the login, so add this at the end of your script
pawn Код:
stock LoginPlayer(playerid,const password[])
{
new EscapedText[60];
mysql_real_escape_string(password, EscapedText);
format(Query,sizeof(Query),"SELECT * FROM `Users` WHERE `Username` = '%s' AND `Password` = '%s'",GetPName(playerid),EscapedText);
mysql_query(Query);
mysql_store_result();
if(mysql_num_rows() != 0)
{
SendClientMessage(playerid,green,"You have been logged in!");
LoadStats(playerid);
}
else
{
SendClientMessage(playerid,red,"Wrong password!");
Kick(playerid);
}
mysql_free_result();
return 1;
}
So as you can see we are using mysql_real_escape_string() again
And this will check if the password is correct or not. Is not kick player, else load his status.
So we are using mysql_num_rows() != 0 again, so it will select the username and his password.
If the password is wrong, it wont select the password.
Add this in the OnDialogResponse
pawn Код:
if(dialogid == Logindialog)
{
if(strlen(inputtext) == 0)
{
ShowPlayerDialog(playerid,Regdialog,DIALOG_STYLE_I NPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
}
else
{
LoginPlayer(playerid,inputtext);
}
}
Here we are just trying to check if he inputs a password, else check if the password is correct.
So on top of OnGameModeInit add this:
pawn Код:
enum PlayerInfo
{
Username[23],
Password[24],
Money
}
new PInfo[MAX_PLAYERS][PlayerInfo]
Thats the enum for your players, where his status will be loaded.
Now lets make it load the status:
pawn Код:
stock LoadStats(playerid)
{
new pName[24],Query[80];
GetPlayerName(playerid,pName,24);
format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s' ", pName);
mysql_query(Query);
mysql_store_result();
mysql_fetch_row_format(Query, "|");
sscanf(Query, "e<p<|>s[24]s[23]i>", PInfo[playerid]);
mysql_free_result();
GivePlayerMoney(playerid,PInfo[playerid][Money]);
return 1;
}
The first s is for his username, the second s is for his password, the third s is for his money.
Then below you can see we are giving his money.
So we are using mysq_fetch_row_format so it selects the selected row in sscanf, then put a |
Then it will separate each row by a | and in sscanf we are selecting 3 rows.

Any questions, or errors?
Ask'em here, ill try my best to help you, even tho im not a mysql genie :b
Reply


Messages In This Thread
Mysql Register/Login system! - by FireCat - 24.12.2011, 17:37
Re: Mysql Register/Login system! - by PlayHard - 24.12.2011, 19:02
Re: Mysql Register/Login system! - by Coffeemonster - 24.12.2011, 19:12
Re: Mysql Register/Login system! - by PlayHard - 24.12.2011, 19:16
Re: Mysql Register/Login system! - by Kar - 24.12.2011, 19:24
Re: Mysql Register/Login system! - by PlayHard - 24.12.2011, 19:28
Re: Mysql Register/Login system! - by BetaLaxx - 24.12.2011, 19:32
Re: Mysql Register/Login system! - by FireCat - 24.12.2011, 19:38
Re: Mysql Register/Login system! - by [HiC]TheKiller - 24.12.2011, 19:53
Re: Mysql Register/Login system! - by FireCat - 24.12.2011, 20:06
Re: Mysql Register/Login system! - by [HiC]TheKiller - 24.12.2011, 20:20
Re: Mysql Register/Login system! - by Gazmull - 25.12.2011, 02:39
Re: Mysql Register/Login system! - by #marcus. - 25.12.2011, 09:08
Re: Mysql Register/Login system! - by Niko_boy - 26.12.2011, 17:14
Re: Mysql Register/Login system! - by =WoR=G4M3Ov3r - 27.12.2011, 01:03
Re: Mysql Register/Login system! - by [HiC]TheKiller - 27.12.2011, 03:52
Re: Mysql Register/Login system! - by FireCat - 27.12.2011, 10:03
Re: Mysql Register/Login system! - by HyperZ - 27.12.2011, 15:57
Re: Mysql Register/Login system! - by FireCat - 27.12.2011, 16:01
Re: Mysql Register/Login system! - by suhrab_mujeeb - 27.12.2011, 16:11
Re: Mysql Register/Login system! - by [DOG]irinel1996 - 29.12.2011, 04:40
Respuesta: Mysql Register/Login system! - by [Nikk] - 29.12.2011, 04:42
Re: Respuesta: Mysql Register/Login system! - by [DOG]irinel1996 - 29.12.2011, 04:53
Re: Respuesta: Mysql Register/Login system! - by FireCat - 29.12.2011, 09:54
Re: Respuesta: Mysql Register/Login system! - by FarSe. - 29.12.2011, 11:36
Respuesta: Mysql Register/Login system! - by [Nikk] - 29.12.2011, 13:49
Re: Respuesta: Mysql Register/Login system! - by Konstantinos - 29.02.2012, 17:01
Re: Mysql Register/Login system! - by FireCat - 29.02.2012, 19:14
Re: Mysql Register/Login system! - by fadhilkab - 15.03.2012, 21:00
Re: Mysql Register/Login system! - by logoster - 06.05.2012, 19:00
Re: Mysql Register/Login system! - by SwiftKidZ - 08.05.2012, 08:46
Re: Mysql Register/Login system! - by vIBIENNYx - 26.05.2012, 21:15
Re: Mysql Register/Login system! - by Mustang[GTS] - 02.08.2012, 19:13
Re: Mysql Register/Login system! - by PawnFox - 10.08.2012, 09:35
Re: Mysql Register/Login system! - by FireCat - 10.08.2012, 09:46
Re: Mysql Register/Login system! - by PawnFox - 10.08.2012, 09:50
Re: Mysql Register/Login system! - by Zilvinass - 10.12.2012, 18:06
Re: Mysql Register/Login system! - by KingPilot - 19.12.2012, 23:41
Re: Mysql Register/Login system! - by DJTunes - 21.12.2012, 18:38
Re: Mysql Register/Login system! - by kamzaf - 21.12.2012, 22:37
Re: Mysql Register/Login system! - by XMoonBladerX - 24.12.2012, 10:09
Re: Mysql Register/Login system! - by michaelcosyns - 18.04.2013, 19:07
Re: Mysql Register/Login system! - by x96664 - 29.06.2013, 20:46
Re: Mysql Register/Login system! - by Alazam - 11.07.2013, 17:26
Re: Mysql Register/Login system! - by Dugzor - 11.09.2013, 18:45
Re: Mysql Register/Login system! - by Yves - 21.12.2013, 19:12
Re: Mysql Register/Login system! - by anou1 - 04.01.2014, 00:58
Re: Mysql Register/Login system! - by anou1 - 05.01.2014, 14:16
Re: Mysql Register/Login system! - by anou1 - 05.01.2014, 21:07
Re: Mysql Register/Login system! - by Ramin - 05.12.2014, 03:02
Re: Mysql Register/Login system! - by cyberlord - 10.12.2014, 22:47
Re: Mysql Register/Login system! - by Vadyanga - 11.12.2015, 01:13
Re: Mysql Register/Login system! - by AndreiWow - 21.04.2016, 18:14

Forum Jump:


Users browsing this thread: 2 Guest(s)