MySQL retrieving Data, G-Styleeez Plugin.
#1

Good evening,

Thanks to anyone whos willing to give me a hand here, I only need help so I know how to do it in the future, I'm a slow learner, however I am willing.

I use G-Styleez example pwn script as a base for my own, just so I learn MYSQL, I tried adding more fields in to my table, after I had WAMP working that is.

pawn Код:
LoginPlayer(playerid,pass[])
{
  new
        pName[MAX_PLAYER_NAME],
        query[256],
        string[256];

    GetPlayerName(playerid,pName,sizeof(pName));
    MySQLCheck();
   
    mysql_real_escape_string(pName,pName);
    mysql_real_escape_string(pass,pass);
    format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE Username = '%s' AND Password = md5('%s') LIMIT 1",pName,pass);
    mysql_query(query);
    mysql_store_result();

    if(mysql_num_rows() == 1)
    {
      new pName1[MAX_PLAYER_NAME];
      GetPlayerName(playerid,pName1,sizeof(pName1));
      format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE Username = '%s' LIMIT 1", pName1);
        mysql_query(query);
        new datafield[10],row[100];
        mysql_fetch_row(row);
        mysql_fetch_field_row(datafield,"Money");
        PlayerInfo[playerid][Money] = strval(datafield);
        mysql_store_result();
        mysql_fetch_field_row(datafield,"Admin");
        PlayerInfo[playerid][Admin] = strval(datafield);
        mysql_store_result();
        mysql_fetch_field_row(datafield,"VIP");
        PlayerInfo[playerid][VIP] = strval(datafield);
        mysql_store_result();
        GivePlayerMoney(playerid,PlayerInfo[playerid][Money]);
        mysql_free_result();
        LoggedIn[playerid] = true;
        format(string,sizeof(string),"      You are now logged in!");
        SendClientMessage(playerid,NOTICE,string);
        return 1;
    }
    else
    {
      Wrongattempt[playerid] += 1;
      printf("Bad log in attempt by %s (Total attempts: %d)",pName,Wrongattempt[playerid]);
        if(Wrongattempt[playerid] >= 3)
        {
      mysql_free_result();
            return KickEx(playerid, "System", "3 Bad Login Attempts");
        }
        mysql_free_result();
        SendClientMessage(playerid,ERROR,"      Wrong Password!");
    }
    return 1;
}
I'd appreciate it if someone could tell me how to get that to retrieve the registered users data, at the moment it just crashes the server, I know the error is somewhere within here.

pawn Код:
new pName1[MAX_PLAYER_NAME];
      GetPlayerName(playerid,pName1,sizeof(pName1));
      format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE Username = '%s' LIMIT 1", pName1);
        mysql_query(query);
        new datafield[10],row[100];
        mysql_fetch_row(row);
        mysql_fetch_field_row(datafield,"Money");
        PlayerInfo[playerid][Money] = strval(datafield);
        mysql_store_result();
        mysql_fetch_field_row(datafield,"Admin");
        PlayerInfo[playerid][Admin] = strval(datafield);
        mysql_store_result();
        mysql_fetch_field_row(datafield,"VIP");
        PlayerInfo[playerid][VIP] = strval(datafield);
        mysql_store_result();
        GivePlayerMoney(playerid,PlayerInfo[playerid][Money]);
        mysql_free_result();
Thanks a lot for anyone who helps, it is greatly appreciated.
Reply
#2

pawn Код:
if(mysql_num_rows() != 0)
    {
      new pName1[MAX_PLAYER_NAME];
      GetPlayerName(playerid,pName1,sizeof(pName1));
      format(query,sizeof(query),"SELECT * FROM `/*tablename*/` WHERE Username = '%s' LIMIT 1", pName1);
        mysql_query(query);
        mysql_store_result();

        new resultline[512],player[X][32];
        mysql_fetch_field_row(resultline,"|");
        split(resultline, player, '|');

        PlayerInfo[playerid][Money] = strval(player[0]);
        PlayerInfo[playerid][Admin] = strval(player[1]);

        //other variables

        mysql_free_result();
        LoggedIn[playerid] = true;
        SendClientMessage(playerid,NOTICE,"     You are now logged in!");
        return 1;
    }
Of course if you are selecting everything (*) it will load everything, so if first thing in the table is not money, you cannot use 'strval(player[0]) for money, but something higher (actual money field) like 'strval(player[5])'.
Reply
#3

Thanks a lot for the help, and the fast reply, after optimising the code you sent me to suit my script I have found I have a different version of strtok, mine has only 2 paramteres, and I can't seem to find the download with one for 3.

Thanks a lot.
Reply
#4

Quote:
Originally Posted by Wazza!
Thanks a lot for the help, and the fast reply, after optimising the code you sent me to suit my script I have found I have a different version of strtok, mine has only 2 paramteres, and I can't seem to find the download with one for 3.
Strtok? Who is talking about strtok? We are in 21th century

Use split (as I have shown) or sscanf, a lot easier, better and faster.
Reply
#5

Quote:
Originally Posted by $ЂЯĢ
Quote:
Originally Posted by Wazza!
Thanks a lot for the help, and the fast reply, after optimising the code you sent me to suit my script I have found I have a different version of strtok, mine has only 2 paramteres, and I can't seem to find the download with one for 3.
Strtok? Who is talking about strtok? We are in 21th century

Use split (as I have shown) or sscanf, a lot easier, better and faster.
Sorry, that is what I meant, I just remeber seeing named strtok before (Same function) which made me call it strtok.

If you could send me the code for split, id be very greatful.

Thank you.
Reply
#6

tohardtosearch?
https://sampwiki.blast.hk/wiki/Code_Snippets#Split
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)