MySql not working need help
#1

I am trying to make a registration using MySql following tutorials,my registration system doesn't work


Код:
#define mysql_host "localhost"
#define mysql_user "root"
#define mysql_password ""
#define mysql_database "test"

public OnPlayerConnect(playerid)
{
	new Query[80];
	GetPlayerName(playerid,pName,24);
	format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
	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,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Log in",string,"Login","");
	}
	else
	{
		format(string,sizeof(string),"Hey, %s! \nYour account is not registered. \nPlease register to continue!",pName);
		ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register",string,"Register","");
	}
	mysql_free_result();
	return 1;
}

stock LoginPlayer(playerid,const password[])
{
	new EscapedText[60],pName[24];
	new Query[80];
	GetPlayerName(playerid,pName,24);
	mysql_real_escape_string(password, EscapedText);
	format(Query,sizeof(Query),"SELECT * FROM `users` WHERE `Username` = '%s' AND `Password` = '%s'",pName,EscapedText);
	mysql_store_result();
	if(mysql_num_rows() != 0)
	{
		SendClientMessage(playerid,COLOR_GREEN,"You have been logged in!");
		LoadStats(playerid);
	}
	else
	{
		SendClientMessage(playerid,COLOR_RED,"Wrong password!");
		Kick(playerid);
	}
	mysql_free_result();
	return 1;
}

stock LoadStats(playerid)
{
	new pName[24],Query[80];
	GetPlayerName(playerid,pName,24);
	format(Query, sizeof(Query), "SELECT * FROM `users` WHERE `Username` = '%s' ", pName);
	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;
}

if(dialogid == DIALOG_REGISTER)
	{
		if(strlen(inputtext) == 0)
		{
			ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
		}
		else
		{
			new EscapedText[60],Query[80],pName[24];
			GetPlayerName(playerid,pName,24);
			mysql_real_escape_string(inputtext, EscapedText);
			format(Query,sizeof(Query),"INSERT INTO `users` (Username,Password,Money) VALUES ('%s','%s,'0')",pName,EscapedText);
			SendClientMessage(playerid,COLOR_GREEN,"You have been successfully registered!");
			GivePlayerMoney(playerid,5000);
			SetPlayerScore(playerid,100);
		}
	}
	if(dialogid == DIALOG_LOGIN)
	{
		if(strlen(inputtext) == 0)
		{
			ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
		}
		else
		{
			LoginPlayer(playerid,inputtext);
		}
	}
Reply
#2

That's because you are just formatting variable named Query, you forgot to do mysql_query
You have to do the following after the format line
Код:
mysql_query(Query);
Reply
#3

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
That's because you are just formatting variable named Query, you forgot to do mysql_query
You have to do the following after the format line
Код:
mysql_query(Query);
Yes i forgot to say that i did that before but its giving me error

error 035: argument type mismatch (argument 1)
Reply
#4

Код:
mysql_query(Your Connection Handler, Query);
Reply
#5

Quote:
Originally Posted by bondowocopz
Посмотреть сообщение
Код:
mysql_query(Your Connection Handler, Query);
what do you mean by connection handler,sorry i am new in this
Reply
#6

Under OnGameModeInit, you will have something like:
pawn Код:
ConnectionHandle = mysql_connect(...);
You use ConnectionHandle as the connection handler. Please don't take this and dump it into your gamemode, it will be different.
Reply
#7

Quote:
Originally Posted by BleverCastard
Посмотреть сообщение
Under OnGameModeInit, you will have something like:
pawn Код:
ConnectionHandle = mysql_connect(...);
You use ConnectionHandle as the connection handler. Please don't take this and dump it into your gamemode, it will be different.
still can't understand :

can you help me with an example

Код:
new Query[80];
	GetPlayerName(playerid,pName,24);
	format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
	mysql_query(Mysql,Query);
	mysql_store_result();
Reply
#8

No, that's not even remotely close to what I said.
I said under OnGameModeInit not OnPlayerConnect.

And what do you mean "can you help me with an example" I gave you a perfectly reasonable one above.
Reply
#9

I tried,this is what i have done

new MySql = -1;

OnGameModeInit()
MySql = mysql_connect(mysql_host,mysql_user,mysql_database ,mysql_password);



new Query[80];
GetPlayerName(playerid,pName,24);
format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
mysql_query(MySql,Query);

but still its not saving my username everytime i reconnect its asks me to register
Reply
#10

pawn Код:
new MySQLConnection;
pawn Код:
MySQLConnection = mysql_connect(MySQL_Host, MySQL_User, MySQL_DB, MySQL_Pass);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)