MySQL problem
#1

Hey guys, what is wrong with this code?
Код:
public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
{
    if(_:playertextid != INVALID_TEXT_DRAW)
	{
	    new Query[80],pName[24],string[164];
	    GetPlayerName(playerid,pName,24);
	    
		if(playertextid == loginscreen[8][playerid])
		{
		    format(Query,sizeof(Query),"SELECT `Username` FROM `players` WHERE `Username` = '%s' LIMIT 1;",pName);
			mysql_query(Query);
			mysql_store_result();
			if(mysql_num_rows() != 0)
			{
				format(string,sizeof(string),"Welcome back, %s! \nPlease type your password below to log in!",pName);
				ShowPlayerDialog(playerid,2016,DIALOG_STYLE_INPUT,"Login",string,"Login","");
			}
			else
			{
				ShowPlayerDialog(playerid, 5, DIALOG_STYLE_MSGBOX,"ERROR","This username is not registered! \nPress on Register to register this username.","OK","");
			}
		}
		else if(playertextid == loginscreen[9][playerid])
		{
			if(mysql_num_rows() == 0)
			{
				format(string,sizeof(string),"Welcome, %s! \nPlease type a password below to register!",pName);
				ShowPlayerDialog(playerid,2017,DIALOG_STYLE_INPUT,"Register",string,"Register","");
			}
			else
			{
				ShowPlayerDialog(playerid, 6, DIALOG_STYLE_MSGBOX,"ERROR","This username is already registered! \nPress on Login to log in...","OK","");
			}
		}
	}
    return 1;
}

So, at the first textdraw I tried to make if he press on Login, that would be loginscreen[8] it will check if his account is registered.

if(mysql_num_rows() != 0)
means if there are more than 0 rows execute this: (right?)
Код:
{
				format(string,sizeof(string),"Welcome back, %s! \nPlease type your password below to log in!",pName);
				ShowPlayerDialog(playerid,2016,DIALOG_STYLE_INPUT,"Login",string,"Login","");
			}
and if there are 0 rows execute this:
Код:
ShowPlayerDialog(playerid, 5, DIALOG_STYLE_MSGBOX,"ERROR","This username is not registered! \nPress on Register to register this username.","OK","");
And, I joined the server with a not registered name, clicked on login button and the login dialog showed, it means that there are more than 0 rows? but how because there is no registered account..



#EDIT

I added (rows == 1) and (rows == 0), now if I join with a unregistered account it won't let me log in anymore but won't let me register either, it will say that I already have an account.
Code:
Код:
if(_:playertextid != INVALID_TEXT_DRAW)
	{
	    new Query[80],pName[24],string[164];
	    new rows = mysql_num_rows();
	    GetPlayerName(playerid,pName,24);
	    
		if(playertextid == loginscreen[8][playerid])
		{
		    format(Query,sizeof(Query),"SELECT `Username` FROM `players` WHERE `Username` = '%s' LIMIT 1;",pName);
			mysql_query(Query);
			mysql_store_result();
			if(rows == 1)
			{
				format(string,sizeof(string),"Welcome back, %s! \nPlease type your password below to log in!",pName);
				ShowPlayerDialog(playerid,2016,DIALOG_STYLE_INPUT,"Login",string,"Login","");
			}
			else
			{
				ShowPlayerDialog(playerid, 5, DIALOG_STYLE_MSGBOX,"ERROR","This username is not registered! \nPress on Register to register this username.","OK","");
			}
		}
		else if(playertextid == loginscreen[9][playerid])
		{
		    format(Query,sizeof(Query),"SELECT `Username` FROM `players` WHERE `Username` = '%s' LIMIT 1;",pName);
			mysql_query(Query);
			mysql_store_result();
			if(rows == 0)
			{
				format(string,sizeof(string),"Welcome, %s! \nPlease type a password below to register!",pName);
				ShowPlayerDialog(playerid,2017,DIALOG_STYLE_INPUT,"Register",string,"Register","");
			}
			else
			{
				ShowPlayerDialog(playerid, 6, DIALOG_STYLE_MSGBOX,"ERROR","This username is already registered! \nPress on Login to log in...","OK","");
			}
		}
	}
This is what I get in phpmyadmin
Код:
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0001 sec)

Created an account mannually, went in game and tried to log in, it told me that it is not registered..
Reply
#2

You are trying to get number of rows before executing your query, so no wonder it won't work. Also, as the query is the same for both cases, you can move it outside if's:
pawn Код:
if(_:playertextid != INVALID_TEXT_DRAW)
{
    new Query[80],pName[24],string[164];
    new rows;
    GetPlayerName(playerid,pName,24);
   
    if(playertextid == loginscreen[8][playerid] || playertextid == loginscreen[9][playerid]) {
        format(Query,sizeof(Query),"SELECT `Username` FROM `players` WHERE `Username` = '%s' LIMIT 1;", pName);
        mysql_query(Query);
        mysql_store_result();
        rows = mysql_num_rows();
    }

    if(playertextid == loginscreen[8][playerid])
    {
        if(rows == 1)
        {
            format(string,sizeof(string),"Welcome back, %s! \nPlease type your password below to log in!",pName);
            ShowPlayerDialog(playerid,2016,DIALOG_STYLE_INPUT,"Login",string,"Login","");
        }
        else
        {
            ShowPlayerDialog(playerid, 5, DIALOG_STYLE_MSGBOX,"ERROR","This username is not registered! \nPress on Register to register this username.","OK","");
        }
    }
    else if(playertextid == loginscreen[9][playerid])
    {
        if(rows == 0)
        {
            format(string,sizeof(string),"Welcome, %s! \nPlease type a password below to register!",pName);
            ShowPlayerDialog(playerid,2017,DIALOG_STYLE_INPUT,"Register",string,"Register","");
        }
        else
        {
            ShowPlayerDialog(playerid, 6, DIALOG_STYLE_MSGBOX,"ERROR","This username is already registered! \nPress on Login to log in...","OK","");
        }
    }
}
Reply
#3

Quote:
Originally Posted by Misiur
Посмотреть сообщение
You are trying to get number of rows before executing your query, so no wonder it won't work. Also, as the query is the same for both cases, you can move it outside if's:
pawn Код:
if(_:playertextid != INVALID_TEXT_DRAW)
{
    new Query[80],pName[24],string[164];
    new rows;
    GetPlayerName(playerid,pName,24);
   
    if(playertextid == loginscreen[8][playerid] || playertextid == loginscreen[9][playerid]) {
        format(Query,sizeof(Query),"SELECT `Username` FROM `players` WHERE `Username` = '%s' LIMIT 1;", pName);
        mysql_query(Query);
        mysql_store_result();
        rows = mysql_num_rows();
    }

    if(playertextid == loginscreen[8][playerid])
    {
        if(rows == 1)
        {
            format(string,sizeof(string),"Welcome back, %s! \nPlease type your password below to log in!",pName);
            ShowPlayerDialog(playerid,2016,DIALOG_STYLE_INPUT,"Login",string,"Login","");
        }
        else
        {
            ShowPlayerDialog(playerid, 5, DIALOG_STYLE_MSGBOX,"ERROR","This username is not registered! \nPress on Register to register this username.","OK","");
        }
    }
    else if(playertextid == loginscreen[9][playerid])
    {
        if(rows == 0)
        {
            format(string,sizeof(string),"Welcome, %s! \nPlease type a password below to register!",pName);
            ShowPlayerDialog(playerid,2017,DIALOG_STYLE_INPUT,"Register",string,"Register","");
        }
        else
        {
            ShowPlayerDialog(playerid, 6, DIALOG_STYLE_MSGBOX,"ERROR","This username is already registered! \nPress on Login to log in...","OK","");
        }
    }
}
Thanks anyway, I switched to R41-2 and I used this, it works, but tell me if I should use mysql free result or something like that, new to mysql

Код:
public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
{
    if(_:playertextid != INVALID_TEXT_DRAW)
	{
	    new DB_Query[115], pName[MAX_PLAYER_NAME], String[150];
	    GetPlayerName(playerid, pName, sizeof(pName));
		if(playertextid == loginscreen[8][playerid])
		{
		    mysql_format(Database, DB_Query, sizeof(DB_Query), "SELECT * FROM `USERS` WHERE `USERNAME` = '%e' LIMIT 1", pName);
            //if (corrupt_check != Corrupt_Check[playerid]) return Kick(playerid);
            
            if(cache_num_rows() > 0)
			{
				cache_get_value(0, "PASSWORD", PlayerInfo[playerid][pPass], 65);
				cache_get_value(0, "SALT", PlayerInfo[playerid][Salt], 11);

				PlayerInfo[playerid][Player_Cache] = cache_save();

				format(String, sizeof(String), "{FFFFFF}Welcome back, %s.\n\n{0099FF}This account is already registered.\n\
				{0099FF}Please, input your password below to proceed to the game.\n\n", pName);
				ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login System", String, "Login", "Leave");
			}
			else
			{
				SendClientMessage(playerid, -1, "Test");
			}
		}
		else if(playertextid == loginscreen[9][playerid])
		{
		    mysql_format(Database, DB_Query, sizeof(DB_Query), "SELECT * FROM `USERS` WHERE `USERNAME` = '%e' LIMIT 1", pName);
            //if (corrupt_check != Corrupt_Check[playerid]) return Kick(playerid);
            
			if(cache_num_rows() <= 0)
			{
			    format(String, sizeof(String), "{FFFFFF}Welcome %s.\n\n{0099FF}This account is not registered.\n\
				{0099FF}Please, input your password below to proceed to the game.\n\n", pName);
				ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration System", String, "Register", "Leave");
			}
			else
			{
				SendClientMessage(playerid, -1, "Test2");
			}
		}
	}
    return 1;
}
Reply
#4

In this one you didn't even execute mysql_query...
Reply
#5

Quote:
Originally Posted by Misiur
Посмотреть сообщение
In this one you didn't even execute mysql_query...
I did after I pasted this here, forgot to update this.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)