MySQL Saving
#1

Hello,

I'm having a little problem, whenever I'm saving a INT in my MySQL database I can easy print it with %i.
But whenever I'm trying to save a 'word' in my MySQL database VARCHAR(24) it fails to print.

I print it like this:
pawn Код:
format(str,128, "Faction[%s]",PlayerInfo[playerid][Faction]);
    SendClientMessage(playerid,0xA69A9DFF,str);
But it just displays: <= Empty spaces.
Here some code:

pawn Код:
enum pInfo
{
    Skin],
    Faction[20],
    Adminlevel
}
pawn Код:
stock MySQL_Login(playerid)
{
    new query[300], pname[24], savingstring[20];
    GetPlayerName(playerid, pname, 24);
    format(query, sizeof(query), "SELECT * FROM playerdata WHERE user = '%s'", pname);
    //We only select the variables that we want to use.
    //We don't need things like the password string or the user string.
    mysql_query(query); //Queries the result
    mysql_store_result(); //Store a result because it's a SELECT statement.
    while(mysql_fetch_row_format(query,"|"))
    {
        //We use while so that it does a single query, not multiple
        //Especially when we have more variables. If there is more
        //Variables, you should just split the line with sscanf. To
        //Make it easier.
        mysql_fetch_field_row(savingstring, "score"); SetPlayerScore(playerid, strval(savingstring));
        mysql_fetch_field_row(savingstring, "money"); MoneyGiven[playerid] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Adminlevel"); PlayerInfo[playerid][Adminlevel] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Skin"); PlayerInfo[playerid][Skin] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Faction"); PlayerInfo[playerid][Faction] = strval(savingstring);
        //If you are wondering why I'm using savingstring instead
        //Of a variable like using MoneyGiven right away, it's because
        //mysql_fetch_field_row requires a string.
    }
    mysql_free_result(); //We must always free a stored result
    SendClientMessage(playerid, -1, "{D1C5C8}[ACCOUNT]{A69A9D} Login process {D1C5C8}succeed!"); //Sends the client a message.
    Logged[playerid] = 1; //Sets our logged in variable to one
    return 1;
}
If you need more code I like to hear that. Just to repeat, whenever I save a number like: 123 it will print with %i. What I'm doing wrong? Thanks.
Reply
#2

The problem is simple, you're storing the return value of strval in the variable. strval is a function for getting the value of a string, therefore it is not required in this case and will not work as your intending it to do. All you need to do is store it directly with your mysql_fetch_field_row function, for example:

pawn Код:
mysql_fetch_field_row(PlayerInfo[playerid][Faction], "Faction");
No need for the strval part for strings, that is simply for "converting" a string to an integer, so to speak.
Reply
#3

Thanks JaTochNietDan it works. In case you didn't know, you are awsome. Thanks.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)