Data from MySQL Database not doing it's job
#1

Hi!

I've got this very strange problem, so I run a query that gets a row of user data (limited to 1 row in the query) and I then use the data from that to assign variables to the active logged in user. However, when I set the player variables (which I've setup using an Enumerator) it doesn't seem to read it, I'm not sure what I'm doing wrong, I'm new to this MySQL Plugin (The new gStylez script, or whatever his new name is).

Here is my query:
pawn Код:
format(string, sizeof(string), "SELECT * FROM `playerinfo` WHERE `userName` LIKE '%s' LIMIT 1", pname);
        mysql_function_query(connectionHandler, string, true, "RestoreData", "i", playerid);
Here is my RestoreData callback.

pawn Код:
forward RestoreData(playerid);
public RestoreData(playerid)
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(!rows)
    {
        SendClientMessage(playerid, COLOR_FAIL, "Unknown Error Occured!");
    } else {
    new tempX[MAX_PLAYERS], tempY[MAX_PLAYERS], tempZ[MAX_PLAYERS], tempBalance[MAX_PLAYERS], tempSkin[MAX_PLAYERS];
    cache_get_field_content(0, "Balance", tempBalance[playerid]);
    cache_get_field_content(0, "x", tempX[playerid]);
    cache_get_field_content(0, "y", tempY[playerid]);
    cache_get_field_content(0, "z", tempZ[playerid]);
    cache_get_field_content(0, "skin", tempSkin[playerid]);
    pInfo[playerid][x] = floatstr(tempX);
    pInfo[playerid][y] = floatstr(tempX);
    pInfo[playerid][z] = floatstr(tempX);
    pInfo[playerid][Money] = strval(tempBalance[playerid]);
    pInfo[playerid][Skin] = strval(tempSkin[playerid]);
    SetSpawnInfo(playerid, 0, pInfo[playerid][Skin], pInfo[playerid][x], pInfo[playerid][y], pInfo[playerid][z], 1, 0, 0, 0, 0, 0, 0);
    pLogged[playerid] = 1;
    SpawnPlayer(playerid);
    ResetPlayerMoney(playerid);
    GivePlayerMoney(playerid, pInfo[playerid][Money]);
    }
}
Yet the variables are not taking an effect, even when I print them they come out as 0, I thought it might have something to do with the fact that they're all integers and cache_get_field_content is for strings but I'm not sure whether or not that would have an effect on it.

If you have any idea please let me know.

Thank you so much.

Kind Regards

Shoulen
Reply
#2

You seem to misunderstand what data is returned and what the concept of the RestoreData(playerid) callback is. There's no reason for you to declare arrays like this:
pawn Код:
new tempX[MAX_PLAYERS];
This does not only unnecessarily clutter up your stack when done repeatedly, but also is useless, since you do not need to handle the result player-specific here. You just need to declare an array large enough to hold a long number and the null character in case of the balance. An example of this would be:
pawn Код:
new temp[11];
// array 'temp' is empty.
cache_get_field_content(0, "Balance", temp);
// array 'temp' is a string in our way of speaking, similar to what a c string is.
// it may look like this: "1337", which would mean the player has a balance of $1337.
// The array contents are {'1', '3', '3', '7', '\0'} (characters 1,3,3,7 and a null character, remaining 6 array members remain empty).
// To get the number representative of this string, you need to run strval:
pInfo[playerid][Money] = strval(temp);
The situation is similar with the next float. You do not need to create an array for each string, by the way! Following the code I posted, you'd need to load the x, y and z. This can be done using cache_get_field_content and floatstr.
pawn Код:
cache_get_field_content(0, "x", temp);
pInfo[playerid][x] = floatstr(temp);
Reply
#3

I've done this:

pawn Код:
new temp[128];
    cache_get_field_content(0, "skin", temp);
    pInfo[playerid][Skin] = strval(temp);
    cache_get_field_content(0, "x", temp);
    pInfo[playerid][x] = floatstr(temp);
    cache_get_field_content(0, "y", temp);
    pInfo[playerid][y] = floatstr(temp);
    cache_get_field_content(0, "z", temp);
    pInfo[playerid][z] = floatstr(temp);
    cache_get_field_content(0, "Balance", temp);
    pInfo[playerid][Money] = strval(temp);
    SetSpawnInfo(playerid, 0, pInfo[playerid][Skin], pInfo[playerid][x], pInfo[playerid][y], pInfo[playerid][z], 1, 0, 0, 0, 0, 0, 0);
    pLogged[playerid] = 1;
    SpawnPlayer(playerid);
In my RestoreData callback, it seems to be working now, thank you so much, I appreciate it a lot.
Reply
#4

Do you use cache_get_field_content on ANY rows that actually contain 128 characters of information at once? If not, then it is better you size down your temp array size. In terms of your script memory management, this variable should not be considered exactly "temporary" as the name might suggest.
Reply
#5

Yeah I know, I'm a little scared though because there are still rows I'm going to be adding, but you're actually right, I could probably get away with much less.
Reply
#6

As long as the data from ANY row that you're loading isn't going to be 128 characters long, you don't need to use 128.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)