Sscanf not assinging values correctly
#1

Hi everyone,

I've been struggling with this problem for a few months now, and I just can't solve it.
I use BlueG's MYSQL plugin, R6, because the others don't work very well for me.
Everything worked fine until I added date saving, but even if I remove it, it won't work.
Here's my loadstats code:
pawn Code:
stock LoadStats(playerid)
{
    new pName[24],Query[800];
    GetPlayerName(playerid,pName,24);
    format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s' ", pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query, "|");
    sscanf(Query, "e<p<|>s[32]s[32]dddddfffddds[128]dddddddddddddddddddddddddddddddddddddddddddds[10]s[20]ddd>", PInfo[playerid]);
    mysql_free_result();
    return 1;
}
I rewrote the sscanf thing a few times, double checked if everything matched, but still, only the money works.
When I print the values, for example PInfo[playerid][Skin], it returns 0, while the debug log says it loaded id 240.
Code:
[11:37:39] CMySQLHandler::FetchRow() - Return: Jari_Johnson|password|5000|6|1|0|0|0|0|0|0|1|0|email@example.com|21|240|0|0|0|0|100|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0||127.0.0.1|3|2|2013

[11:37:39] >> mysql_free_result( Connection handle: 1 )
The thing from the log I use as reference, is the second value after the email, which is 240, the skin.
I hope someone sees a mistake somewhere, because I am out of ideas.

Thanks in advance
Reply
#2

I don't like bumping, but I really can't figure this out :\
Reply
#3

Allright, I will try this tonight, I hope I can find something, thanks for the advice

EDIT: Is there any other way to load this big query with sscanf? Perhaps by splitting the query in 2 or 3 parts?
If so, can you show me any example?
I am not sure about how to resume the variable assinging, from for example PInfo[playerid][Skin] from query part 1, and start with PInfo[playerid][PNumber] in the next query.

EDIT2: I get a string buffer overflow warning, which is expected.
I increased the size, but still the warning appears.
Now, I'm pretty sure that splitting up the query is the way to go, but like my first edit said, I am not totally sure how.
I will experiment with Y_Less's advice, and I hope to get a response on how to split this long query or something..
Thanks in advance
Reply
#4

I have the same problem, but my query is a lot smaller.
pawn Code:
stock LoadVehicles()
{
    new Query[256], count;
    for(new i; i < MAX_VEHICLES; i++)
    {
        format(Query, sizeof(Query), "SELECT * From Vehicles WHERE ID = %d", i);
        print(Query);
        mysql_query(Query);
        mysql_store_result();
        if(mysql_num_rows())
        if(mysql_fetch_row_format(Query, "|"))
        {
            sscanf(Query, "iiffff", vData[i][vehID], vData[i][vehModel], vData[i][XPos], vData[i][YPos], vData[i][ZPos], vData[i][Angle]);
            CreateVehicle(vData[i][vehModel], vData[i][XPos], vData[i][YPos], vData[i][ZPos], vData[i][Angle], random(126), random(126), 60);
            printf("%d %d %f %f %f %f", vData[vehID], vData[i][vehModel], vData[i][XPos], vData[i][YPos], vData[i][ZPos], vData[i][Angle]);
            mysql_free_result();
            count ++;
        }
    }
    printf("%d vehicles were loaded from the database. ", count);
}
Console says this:
Quote:

[13:59:11] SELECT * From Vehicles WHERE ID = 1
[13:59:11] 0 0 0.000000 0.000000 0.000000 0.000000
[13:59:11] SELECT * From Vehicles WHERE ID = 2
[13:59:11] 0 0 0.000000 0.000000 0.000000 0.000000
[13:59:11] SELECT * From Vehicles WHERE ID = 3
[13:59:11] 0 0 0.000000 0.000000 0.000000 0.000000
[13:59:11] SELECT * From Vehicles WHERE ID = 4
[13:59:11] 0 0 0.000000 0.000000 0.000000 0.000000

While mysql_debug tells me this:
Quote:

[14:06:30] CMySQLHandler::Query(SELECT * From Vehicles WHERE ID = 1) - Successfully executed.
[14:06:30] >> mysql_store_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::StoreResult() - Result was stored.
[14:06:30] >> mysql_num_rows( Connection handle: 1 )
[14:06:30] CMySQLHandler::NumRows() - Returned 1 row(s)
[14:06:30] >> mysql_fetch_row_format( Connection handle: 1 )
[14:06:30] CMySQLHandler::FetchRow() - Return: 1|411|4.64056|6.1187|2.86991|52.5
[14:06:30] >> mysql_free_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::FreeResult() - Result was successfully free'd.
[14:06:30] >> mysql_query( Connection handle: 1 )
[14:06:30] CMySQLHandler::Query(SELECT * From Vehicles WHERE ID = 2) - Successfully executed.
[14:06:30] >> mysql_store_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::StoreResult() - Result was stored.
[14:06:30] >> mysql_num_rows( Connection handle: 1 )
[14:06:30] CMySQLHandler::NumRows() - Returned 1 row(s)
[14:06:30] >> mysql_fetch_row_format( Connection handle: 1 )
[14:06:30] CMySQLHandler::FetchRow() - Return: 2|411|8.7544|2.96196|2.8367|52.4999
[14:06:30] >> mysql_free_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::FreeResult() - Result was successfully free'd.
[14:06:30] >> mysql_query( Connection handle: 1 )
[14:06:30] CMySQLHandler::Query(SELECT * From Vehicles WHERE ID = 3) - Successfully executed.
[14:06:30] >> mysql_store_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::StoreResult() - Result was stored.
[14:06:30] >> mysql_num_rows( Connection handle: 1 )
[14:06:30] CMySQLHandler::NumRows() - Returned 1 row(s)
[14:06:30] >> mysql_fetch_row_format( Connection handle: 1 )
[14:06:30] CMySQLHandler::FetchRow() - Return: 3|411|15.1183|-1.9213|2.84453|52.5
[14:06:30] >> mysql_free_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::FreeResult() - Result was successfully free'd.
[14:06:30] >> mysql_query( Connection handle: 1 )
[14:06:30] CMySQLHandler::Query(SELECT * From Vehicles WHERE ID = 4) - Successfully executed.
[14:06:30] >> mysql_store_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::StoreResult() - Result was stored.
[14:06:30] >> mysql_num_rows( Connection handle: 1 )
[14:06:30] CMySQLHandler::NumRows() - Returned 1 row(s)
[14:06:30] >> mysql_fetch_row_format( Connection handle: 1 )
[14:06:30] CMySQLHandler::FetchRow() - Return: 4|411|23.3501|-8.2377|2.84427|52.4999
[14:06:30] >> mysql_free_result( Connection handle: 1 )
[14:06:30] CMySQLHandler::FreeResult() - Result was successfully free'd.

Reply
#5

If I put "print", it prints the text with no problems, so the problem is at the sscanf line. Either me using the plugin wrong or something else. Also I am on Windows so no I can't.
Reply
#6

Quote:
Originally Posted by Y_Less
View Post
Oh wait, I know your problem - you missed "p<|>"!
Oh well, I feel stupid now. But now it works perfectly - thanks. And sorry OP for hijacking your thread and good luck with fixing your script.
Reply
#7

Quote:
Originally Posted by spedico
View Post
Oh well, I feel stupid now. But now it works perfectly - thanks. And sorry OP for hijacking your thread and good luck with fixing your script.
No problem, it's good that your problem is solved
I had no time until now to try Y_Less's advice, but I have now, so I will update this post when i have some results
Reply
#8

Well, I stripped down my query like you said, and I keep getting the string buffer overflow error in my console, even though there is just one value loaded, and the query is 1000.
It only dissapears if I delete everything in my sscanf query.
The error persists here:

pawn Code:
new pName[24],Query[1000];
    GetPlayerName(playerid,pName,24);
    format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s' ",pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query);
    sscanf(Query, "e<p<|>s[32]>", PInfo[playerid]);
    mysql_free_result();
and it dissapears here:
pawn Code:
new pName[24],Query[1000];
    GetPlayerName(playerid,pName,24);
    format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s' ",pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query);
    sscanf(Query, "e<p<|>>", PInfo[playerid]);
    mysql_free_result();
Now, it seems like there is allways a string buffer overflow, even though everywhere the assigned size is the same :\ (like Username[32], which is the first value)
Reply
#9

Quote:
Originally Posted by Y_Less
View Post
That's because the "s" is the last parameter so is treated differently - add a space after it to change that.
so instead of s[32]s[32] it should be like s[32] s[32] ?
Reply
#10

I'm afraid I don't get it :$
The end of the specifier, is that like "e<p<|>s[32]s[32] >", one char before the > ?

EDIT: Allright, I got rid of the overflow error, but every value I print for example the email variable, gives (null) as result..
I have no idea what the problem is here
This is my current code, like I said, I increased some sizes(i know, it's way too much, but I just did this for the debugging process).
pawn Code:
new pName[24],Query[1000];
    GetPlayerName(playerid,pName,24);
    format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s' ",pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query, "|");
    sscanf(Query, "e<p<|>s[128]s[128]dddddfffddds[128]dddddddddddddddddddddddddddddddddddddddddddds[10]s[20]ddd>", PInfo[playerid]);
    mysql_free_result();
Reply
#11

*Bump*
I added stuff to the post above this one, I hope someone can help me solve this, so I can continue my developments.
Reply
#12

Quote:
Originally Posted by Y_Less
View Post
Did you increase the size of the corresponding items in the enum? And that's not a minimal example like I asked for.
Ups, sorry Y_Less :$
Actually, what you said about the enum sizes got me thinking, so I took a look at my MYSQL structure tab, and saw that the size of the rows in question were still 32, so now I changed them to 128, like the in the sscanf function, and I spawned with my skin ID 240..
I am now checking if every variable is loaded correctly, and so far it looks good!
I will edit this post after I'm done, or I will post again if someone else posted after me, to say if it's solved.
In any way, thanks alot Y_Less

EDIT: the first until the last variable are loaded succesfully, problem solved
Thanks again Y_Less!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)