MySQL Saving
#1

Okay guys, I am having a problem.

I am trying to make a system in which you can have a username, and create several characters within them (5).

Now, the problem is, it won't save. I mean, when it goes back to the dialog to show what characters are there, they saved them there correctly, yet, they will not save into the database.

This is the line of code that saves the new character you register.

pawn Код:
stock RegisterCharacter(playerid, charactername[24])
{
    new query[200];
    if(!strcmp(CharacterInfo[playerid][0], "None", false)) CharacterInfo[playerid][0] = charactername;
    else if(!strcmp(CharacterInfo[playerid][1], "None", false)) CharacterInfo[playerid][1] = charactername;
    else if(!strcmp(CharacterInfo[playerid][2], "None", false)) CharacterInfo[playerid][2] = charactername;
    else if(!strcmp(CharacterInfo[playerid][3], "None", false)) CharacterInfo[playerid][3] = charactername;
    else if(!strcmp(CharacterInfo[playerid][4], "None", false)) CharacterInfo[playerid][4] = charactername;
    format(query, sizeof(query), "INSERT INTO playerdata(name, Username, Cash, PlayHours) VALUES('%s', '%s', 0, 0)", charactername, pInfo[playerid][Username]);
    mysql_query(query);
    SavePlayerCharacters(playerid);
    ShowDialog(playerid, 3);
    return 1;
}

//SavePlayerCharacters stock
stock SavePlayerCharacters(playerid)
{
    if(Logged[playerid] == 1)
    {
        new query[200];
        format(query, sizeof(query), "UPDATE Accounts SET Character1=%s, Character2=%s, Character3=%s, Character4=%s, Character5=%s, Adminlevel=%i WHERE Username='%s'", \
        CharacterInfo[playerid][0], CharacterInfo[playerid][1], CharacterInfo[playerid][2], CharacterInfo[playerid][3], CharacterInfo[playerid][4], pInfo[playerid][Username]);
        mysql_query(query);
    }
    return 1;
}
Also, if you can try to make it look nicer, because the way it looks now doesn't look very efficient, it would be appreciated.

Thanks in advance,
- Kindred.

PS: I am a newbie at MySQL, haven't used it in a few months and only started it a few months ago.

PS2: How can I see how much of a query size I need. I am new to this and would really like to know this before using useless memory.
Reply
#2

Once again: If you're having trouble with MySQL: Enable the debug!
pawn Код:
mysql_debug(1);
Query size can be calculated by counting the characters, subtracting the placeholders (%s, %d, etc) and then adding how much space a variable would take. For integers: +11 each, for playernames +24 each. For every other variable you'll need to see for yourself. You can count characters by looking in the right hand bottom corner in Pawno. Place your cursor at the start of the query and make note of the Col: value. Do the same at the end of the query and subtract one from another. http://puu.sh/FMpY
Reply
#3

Thanks, I'm getting this error:

Код:
[18:29:10] CMySQLHandler::Query(UPDATE Accounts SET Character1=Kurt_Siddude, Character2=Kurt_Siddudee, Character3=None, Character4=None, Character5=None, Adminlevel=75 WHERE Username='') - An error has occured. (Error ID: 1054, Unknown column 'Kurt_Siddude' in 'field list')
Does anyone know how to fix it? Like I said, I am new to mysql.

Ohh, and does anyone know why Adminlevel shows up as "NULL" instead of "0" or whatever number it is set to?
Reply
#4

pawn Код:
stock SavePlayerCharacters(playerid)
{
    if(Logged[playerid] == 1)
    {
        new query[200];
        format(query, sizeof(query), "UPDATE Accounts SET Character1='%s', Character2='%s', Character3='%s', Character4='%s', Character5='%s', Adminlevel=%i WHERE Username='%s'", \
        CharacterInfo[playerid][0], CharacterInfo[playerid][1], CharacterInfo[playerid][2], CharacterInfo[playerid][3], CharacterInfo[playerid][4], pInfo[playerid][Username]);
        mysql_query(query);
    }
    return 1;
}
You need to put single quotes around the string specifier, so it would look like the code above ('%s' not %s)
Reply
#5

Now I'm getting this:

Код:
[18:41:35] CMySQLHandler::Query(UPDATE Accounts SET Character1='Kurt_Sidman', Character2='Kurt_Othername', Character3='None', Character4='None', Character5='None', Adminlevel=75 WHERE Username='') - Successfully executed.
Looks like it is working, yet it is still not saved.
Reply
#6

Your pInfo[playerid][Username] variables seems to be null. This under the query and see what you get.
pawn Код:
printf("Saved stats for %s",pInfo[playerid][Username]);
Reply
#7

It says:

Код:
Saved stats for Kurt_Sidman
Which is exactly what my username is.
Reply
#8

You forgot the adminlevel variable in your format function
Reply
#9

Quote:
Originally Posted by FUNExtreme
Посмотреть сообщение
You forgot the adminlevel variable in your format function
He has it, but this all beats me...everything seems fine to me.
Reply
#10

Quote:
Originally Posted by FUNExtreme
Посмотреть сообщение
You forgot the adminlevel variable in your format function
Allow me to be more specific..

I added something to your query..

pawn Код:
format(query, sizeof(query), "UPDATE Accounts SET Character1='%s', Character2='%s', Character3='%s', Character4='%s', Character5='%s', Adminlevel=%i WHERE Username='%s'", \
        CharacterInfo[playerid][0], CharacterInfo[playerid][1], CharacterInfo[playerid][2], CharacterInfo[playerid][3], CharacterInfo[playerid][4], YOUAREMISSINGTEHEADMINLEVELHERE, pInfo[playerid][Username]);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)