SA-MP Forums Archive
MySQL account issue - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: MySQL account issue (/showthread.php?tid=112135)



MySQL account issue - jamesb93 - 05.12.2009

Hey,

Well I have a problem, With my sql gamemode. Sometimes when you connect even if you are registered it thinks your not and makes you re-register, causing the database to have multiple tables for the same person.

Here is some of the relevant code:

pawn Код:
public OnGameModeInit()
{
    //===> MySQL CONNECTION <===
    mysql_debug(1); //1 if you want to have log of all mysql actions; 0 if you don't want it
    new connection = mysql_connect(DB_IP,DB_USER,DB_NAME,DB_PASS);
    if(connection)
    {
      new stats[256];
      mysql_stat(stats);
      print(stats);
      print("[mysql] MySQL connection successfull!");
    }
    else
    {
      print("[mysql] MySQL connection un-successfull; server closed ...");
      return GameModeExit();
    }
    //===========================
    for(new x=0; x<MAX_VEHICLES; x++)
    {
      vInfo[x][vBuyAble] = 0;
    }
pawn Код:
public OnPlayerConnect(playerid)
{
    new
  string[128],
  PlayerName[24];
    GetPlayerName(playerid, PlayerName, sizeof(PlayerName));


  TextDrawShowForPlayer(playerid, Wesbite);
  TextDrawShowForPlayer(playerid, Logo);
  TextDrawShowForPlayer(playerid, Name);
    new pName[MAX_PLAYER_NAME];
  GetPlayerName(playerid, pName, sizeof(pName));
  format(string, sizeof(string), "%s has joined the server.", pName);
  SendClientMessageToAll(COLOR_LIGHTRED, string);

  SendClientMessage(playerid, LIGHTGREEN, "[NotimeBot(v1.1)]: Starting up Notime Defense System...");
  SetTimerEx("NTDSstart", 10000, 0, "i", playerid);
  Report[playerid] = 0;

    SetWeather(10);

    //===> MySQL CHECK <===
    PlayerInfo[playerid][pSQLId] = INVALID_SQL_ID;
    FindPlayerSQL(playerid);
    if(PlayerInfo[playerid][pSQLId] > INVALID_SQL_ID)
    {
        gPlayerAccount[playerid] = 1;
        ShowLoginDialog(playerid);
    }
    else
    {
        gPlayerAccount[playerid] = 0;
        ShowRegDialog(playerid);
    }
    //====================
Anyone got ideas why it's doing this?
pawn Код:
public CheckSQL()
{
  if(mysql_ping() == (-1))
    {
        mysql_connect(DB_IP,DB_USER,DB_NAME,DB_PASS);
        print("[mysql] MySQL connection lost! Reconnecting ...");
    }
    return 1;
}



Re: MySQL account issue - Grim_ - 05.12.2009

I never see you check whether the database already has a table/row for that player or not.


Re: MySQL account issue - jamesb93 - 05.12.2009

Quote:
Originally Posted by _Xerxes_
I never see you check whether the database already has a table/row for that player or not.
Can someone explain how to do that?

Also I heard that "PlayerInfo[playerid][pSQLId] = INVALID_SQL_ID;" dosent work becuase the player isent logged in yet.


Re: MySQL account issue - Grim_ - 05.12.2009

Something like this: (Copied directly from rAdmin script)
pawn Код:
public MySQLCheckUserExistance(playerid)
{
    GetPlayerName(playerid, PlayerName, sizeof(PlayerName));

    format(query, sizeof(query), "SELECT `reg_id` FROM `users` WHERE `name` = '%s'", PlayerName);
    samp_mysql_query(query);
    samp_mysql_store_result();

    if(samp_mysql_num_rows() == 0)
    {
        return 0;
    }
    else
    {
      return 1;
    }
}
Usage:
pawn Код:
if(MySQLCheckUserExistance(playerid))
{
  //User is in the database (exists)
}



Re: MySQL account issue - jamesb93 - 05.12.2009

Already have this

pawn Код:
public FindPlayerSQL(playerid)
{
    CheckSQL();
    new query[128],pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid,pname,sizeof(pname));
    mysql_real_escape_string(pname,pname);
    format(query,sizeof(query),"SELECT playerid FROM `players` WHERE name='%s' LIMIT 1",pname);
    mysql_query(query);
    mysql_store_result();
    PlayerInfo[playerid][pSQLId] = mysql_fetch_int();
    mysql_free_result();
    return 1;
}



Re: MySQL account issue - Grim_ - 05.12.2009

I think that is different from what I have posted. Try using MySQLCheckUserExistance to see if the player exists or not.


Re: MySQL account issue - jamesb93 - 06.12.2009

Back again, /sigh

Okay I have this now:
pawn Код:
public MySQLCheckUserExistance(playerid)
{
    new PlayerName[128];
    new query[265];
    GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
    format(query, sizeof(query), "SELECT `reg_id` FROM `users` WHERE `name` = '%s'", PlayerName);
    mysql_query(query);

    if(mysql_num_rows() == 0)
    {
        return 0;
    }
    else
    {
      return 1;
    }
}
and

pawn Код:
//===> MySQL CHECK <===
    MySQLCheckUserExistance(playerid);
    PlayerInfo[playerid][pSQLId] = INVALID_SQL_ID;
    FindPlayerSQL(playerid);
    if(MySQLCheckUserExistance(playerid))
    {
        gPlayerAccount[playerid] = 1;
        ShowLoginDialog(playerid);
    }
    else
    {
        gPlayerAccount[playerid] = 0;
        ShowRegDialog(playerid);
    }
    //====================
Now I think if your registered it works fine but if your a new player you get the login dialog.


Re: MySQL account issue - jamesb93 - 06.12.2009

Anyone?


Re: MySQL account issue - Sergei - 06.12.2009

MySQLCheckUserExistance(playerid) only tells you if that username already exists in table or not, while we need that user's ID for later reference that's why there is function FindPlayerSQL.

Could it be that mysql_fetch_int is bugged? Please check mysql_log when that query is executed, user exists but it still shows you registration dialog.