MySQL Problems
#1

Alright, I can usually mess around and figure out stuff when I'm scripting. However, mysql has me stumped. I thought I was doing it right from what I learned in the three hours I was doing it. I have the following in my script:

Код:
public OnPlayerConnect(playerid)
{
	PlayerConnection(playerid);
	return 1;
}

stock ResetPlayerVariables( playerid )
{
    Account[playerid][AccID] = 0;
    //Account[playerid][PosX] = 0.00;
    return 1;
}


stock PlayerConnection(playerid)
{
  ResetPlayerVariables(playerid);
  new Query[128], Name[MAX_PLAYER_NAME], Name2[MAX_PLAYER_NAME];

	mysql_real_escape_string(Name,Name2);
	#pragma unused Name

	format(Query,sizeof(Query), "SELECT `UserID` FROM `Accounts` WHERE `Username` = '%s'", Name2);
	mysql_query(Query);
	mysql_store_result();
	if(mysql_num_rows() >= 1 )
	{
 		if(mysql_num_rows() >= 2 )
  		{
		 	mysql_free_result();
		 	SendClientMessage(playerid, COLOR_LIGHTRED, "** [Error]: Two Accounts found, please contact a 1337 Admin");
			Kick(playerid);
		}
 		else
 		{
 			Account[playerid][AccID] = mysql_fetch_int();
		  	mysql_free_result();
   		ShowPlayerDialog( playerid, 1, DIALOG_STYLE_INPUT, "Login Process", "Welcome to SF-RP!\nPlease enter your password to continue.", "Login", "Cancel" );
	   	SendClientMessage( playerid, COLOR_YELLOW, "** Your account has been recovered, please enter your password to proceed." );
		}
	}
	else
	{
 		mysql_free_result();
  		SendClientMessage( playerid, COLOR_YELLOW, "** Your account does not yet exist, enter a password to register one!" );
  	ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Welcome to SF-RP\nPlease enter a password to register.", "Register", "Cancel" );
	}
}
Код:
switch( dialogid )
	{
	  case 1:
	  {
			GetFromAccount(Account[playerid][AccID], "Password", Account[playerid][Pass]);
			if( strcmp( inputtext, Account[playerid][Pass], true ) == 0 )
			{
			  new String[ 128 ], Name[ MAX_PLAYER_NAME ];

			  GetPlayerName( playerid, Name, sizeof( Name ) );

			  format( String, sizeof( String ), "** Thank you for logging in, %s.", Name );
			  SendClientMessage( playerid, COLOR_YELLOW, String );
			  LoadAccount(playerid);
			}
			else
			{
			  SendClientMessage( playerid, COLOR_LIGHTRED, "** [Error]: Incorrect password. Please try again!" );
			  ShowPlayerDialog( playerid, 1, DIALOG_STYLE_INPUT, "Login Process", "Welcome to SF-RP!\nPlease enter your password to authenticate.", "Login", "Cancel" );
			}
	  }

	  case 2:
	  {
	    if( strlen(inputtext) >= 30 )
	    {
	      ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Welcome to SF-RP!\nPlease enter a password to register.", "Authenticate", "Cancel" );
	      SendClientMessage( playerid, COLOR_LIGHTRED, "** [Error]: You must use a shorter password - has to be less than 30 characters." );
	    }
	    else
	    {
	      if( strlen(inputtext) >= 1 )
	      {
	        new EscapedPassword[30], Query[128], Name[ MAX_PLAYER_NAME ], EscapedName[ MAX_PLAYER_NAME ];

	        GetPlayerName( playerid, Name, sizeof( Name ) );

	        mysql_real_escape_string( Name, EscapedName );
	        mysql_real_escape_string( inputtext, EscapedPassword );

	        #pragma unused Name

	        format( Query, sizeof( Query ), "INSERT INTO `Accounts` (UserID, Username, Pass) VALUES('%d', '%s', '%s')", Account[playerid][AccID],EscapedName, EscapedPassword );
	        mysql_query(Query);
	        RegAccount(playerid);

	        SendClientMessage( playerid, COLOR_YELLOW, "** Now, please login with the password you've just entered to proceed." );
					PlayerConnection(playerid);
	      }
	      else
	      {
	        ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Welcome to SF-RP!\nPlease enter a password to register.", "Authenticate", "Cancel" );
	        SendClientMessage( playerid, COLOR_LIGHTRED, "** [Error]: Your password must be longer than 1 character..." );
	      }
	    }
	  }
	}
Код:
stock SaveAccount(playerid)
{
	new String[350];
	format( String, sizeof( String ), "UPDATE `Accounts` SET `Blah` = '%d', WHERE `UserID` = '%d'",Account[playerid][Blah],Account[playerid][AccID]);
	mysql_query( String );
	return 1;
}

stock RegAccount(playerid)
{
	new String[350];
	format( String, sizeof( String ), "UPDATE `Accounts` SET `Blah` = '%d', WHERE `UserID` = '%d'",Account[playerid][Blah],Account[playerid][AccID]);
	mysql_query( String );
	return 1;
}

stock LoadAccount(playerid)
{
	if( IsPlayerConnected(playerid))
	{
	  new DataString[128], Query[128];

		format( Query, sizeof( Query ), "SELECT * FROM `Accounts` WHERE `UserID` = '%d'", Account[playerid][AccID]);
		mysql_query(Query);
		mysql_store_result();

    mysql_fetch_field( "Blah", DataString );
	Account[playerid][Blah] = strval( DataString );
    mysql_free_result();
	}
}

stock GetFromAccount( dbid, obtaining[], holdingvar[] )
{
	new Query[128];
	format( Query, sizeof( Query ), "SELECT `%s` FROM `Accounts` WHERE `UserID` = '%d'", obtaining, dbid );
	mysql_query( Query );
	mysql_store_result();

	if( mysql_fetch_row( holdingvar ) == 1 )
	{
	  mysql_free_result();
	}

	return 1;
}
At first, the server crashed when I connected but I fixed that. Now I can connect, register and it'll send the "Login now" message. But then it goes right back to the "You don't have an account" dialog.

The Server Log also says:

Код:
[16:45:09] [join] DeLuca has joined the server (0:192.168.2.2)
[16:45:17] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `UserID` = '0'' at line 1.
[16:45:17] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `UserID` = '0'' at line 1.
[16:45:18] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `UserID` = '0'' at line 1.
Any help is appreciated.
Reply


Forum Jump:


Users browsing this thread: