MySQL string to variable
#1

Hey guys,

Im trying to get some data out of a database, and im not really sure how to return a string.
Right now im using this piece of code:
pawn Код:
cache_get_row( 0, 1, fetch, g_Handle, 24 );
With this code it isnt returning the username out of the database.
It is connected to the right database, since it is loading the coordinates and other data.

Anyone can help me?

~Wesley
Reply
#2

I'm no expert with the mySQL plugin or SQL itself but in my callback I just have this:

pawn Код:
new rows, fields;
cache_get_data(rows, fields);

// Check if account exists
if(rows)
{
    new temp[256];

    // Load an integer
    cache_get_field_content(0, "SomeFieldName", temp);
    pInfo[playerid][some_var] = strval(temp);

    // Load a string directly into an enum/array structure
    cache_get_field_content(0, "AnotherFieldName", pInfo[playerid][some_string]);
}
else
{
    // No account, prompt to register or whatever
}
I wouldn't use the 'field ID' thing as what if you add a field between other fields? Then your entire order messes up.
Reply
#3

Quote:
Originally Posted by MP2
Посмотреть сообщение
I wouldn't use the 'field ID' thing as what if you add a field between other fields? Then your entire order messes up.
That's assuming you use a wildcard in your SELECT queries. Which is as bad as cache_get_field_content or whatever the function is called.
Reply
#4

What's wrong with cache_get_field_content? And if it's so bad, why is it the first piece of code in your tutorial.
Reply
#5

Don't get me wrong, I didn't mean anything bad and what to use is entirely up to the one designing their code.

However my personal opinion always is that people need to know what their code does and how exactly it is done unless using convenient functions isn't going to slow your code down.

Of course I understand that, given the speeds of modern day processors and clever compiler optimizations in C++, talking about performance cost in a situation where there need to be maximally ~50 string comparisons is somewhat stupid, but personally I wouldn't dismiss such "cost" unless working with a larger team where everyone needs to grasp what's written on the fly.

The reason I objected towards the usage of SELECT * is that with larger sets of information, not every piece of data is required in every situation. Once again something extremely trivial and perhaps not going to give a noticeable gain in speed, but if your table has 30 fields and you only need to use 20 of them, retrieve only these 20. I'd say it is just good practice and has another upside to it - you'll know the field "indexes" to use in cache_get_row (or cache_get_row_int, cache_get_row_float).

I mention the function only briefly in my tutorial though. Not because I don't like the function itself, but because my examples aren't really based on that.

Edit: I think there may be a problem with your code, where cache_get_field_content is supposed to fill an enum member! If this function has a parameter calling sizeof() on the enumeration member, it is going to return the actual size of the enum, not the size of the member!

This is close to the situation that the thread author has addressed by using 24 as the last parameter. But it isn't exactly necessary in his case as it is not a member of an enum. Also, should the string sizes change and the array be made larger, every cache_get_row call will need to be modified. sizeof is useful here to avoid that!
Reply
#6

Okay, say I did this:

SELECT field1, field2, field3, field4, field5

Then I select the data with cache_get_row.

If I remove field2, I've then got to change field3 to what field2 was, field4 to what field3 was and field5 to what field4 was. What if I have 100+ fields (very possible in a user table) and I decide to remove a field..?
Reply
#7

Yup, that sounds like a perfectly valid argument for preferring cache_get_field_content.

What you could do to prevent having to modify all the cache_get_row lines in this case would be to replace field2 with field5 for instance, and remove the cache_get_row line for field5. This is just one option though, and it depends largely on what you do with the retrieved data.

By the way, great signature
Reply
#8

Quote:
Originally Posted by AndreT
Посмотреть сообщение
What you could do to prevent having to modify all the cache_get_row lines in this case would be to replace field2 with field5 for instance, and remove the cache_get_row line for field5.
Ah, I never thought of that. Good idea!

Quote:
Originally Posted by AndreT
Посмотреть сообщение
By the way, great signature
Thanks haha

The only reason I use the field names is because it's easier to add stuff - I'm not too bothered (like other people) about shaving a millsecond off something that'll only happen about once a minute (if I'm lucky).
Reply
#9

Ok,

So i changed the cache_get_row to cache_get_field_content, and tried it again. Still it doesnt load.
Aswell i removed the 24, since it wasnt really needed.
Im sure all of the sizes are the same, which is 24. So that shouldnt cause any problems.
Reply
#10

Still need help with this one, anyone knows?
Reply
#11

Uhm, as far as I understood from your question,

pawn Код:
cache_get_row(0, 1, array);
is enough to load the string into the array. After that, you can use the array as you wish.
Reply
#12

Im using that, but as i said already, it doesnt load in the variable, neither in the array
Reply
#13

Quote:
Originally Posted by Wesley221
Посмотреть сообщение
Im using that, but as i said already, it doesnt load in the variable, neither in the array
Try executing the query on phpmyadmin (its probably installed if you're using a third party localhost installer) and check if its getting executed without any problems.
If you succeed on executing, can you get the detailed log of the action you're trying to do, with mysql_debug enabled and put on here?
Reply
#14

Query gets executed, since all the other data is getting loaded.

Код:
[20:23:02] ProcessQueryThread(SendQuery) - Executing query UPDATE vehicles SET username = '', modelid = 603, xpos = 2146.639892, ypos = 2751.100097, zpos = 10.843799, apos = 88.641098, color1 = 38, color2 = 19, locked = 0 WHERE carid = 2...
[20:23:02] ProcessQueryThread(SendQuery) - Query was successful.
[20:23:02] ProcessQueryThread(SendQuery) - Data being passed to ProcessTick().
[20:23:02] SendQuery() - Callback is being called...
Reply
#15

Quote:

UPDATE vehicles SET username = ''

You are trying to update the username field on vehicles table to an empty string here. Plus, if you want to load a data from MySQL you have to apply "SELECT" query with caching enabled.
Reply
#16

Dayum, posted the wrong one; my bad
Код:
[20:32:24] ProcessQueryThread(OnVehicleLoad) - Executing query SELECT * FROM vehicles WHERE carid = 1...
[20:32:24] ProcessQueryThread(OnVehicleLoad) - Query was successful.
[20:32:24] ProcessQueryThread(OnVehicleLoad) - Data caching enabled.
[20:32:24] CMySQLHandler::StoreResult() - Result was stored.
[20:32:24] CMySQLHandler::FreeResult() - Result was successfully freed.
[20:32:24] ProcessQueryThread(OnVehicleLoad) - Data being passed to ProcessTick().
[20:32:24] OnVehicleLoad(i) - Callback is being called...
[20:32:24] >> cache_get_data(Connection handle: 1)
[20:32:24] ProcessTick() - The cache has been cleared.
This is the one to load a vehicle.
pawn Код:
forward OnVehicleLoad( vehicleID );
public OnVehicleLoad( vehicleID )
{
    new rows, fields;
    cache_get_data( rows, fields, g_Handle );
    if( rows )
    {
        //new fetch[24];
        VehicleInfo[vehicleID][vModel]              = cache_get_row_int( 0, 2, g_Handle );
        VehicleInfo[vehicleID][vLoc][0]             = cache_get_row_float( 0, 3, g_Handle );
        VehicleInfo[vehicleID][vLoc][1]             = cache_get_row_float( 0, 4, g_Handle );
        VehicleInfo[vehicleID][vLoc][2]             = cache_get_row_float( 0, 5, g_Handle );
        VehicleInfo[vehicleID][vLoc][3]             = cache_get_row_float( 0, 6, g_Handle );
        VehicleInfo[vehicleID][vColor1]             = cache_get_row_int( 0, 7, g_Handle );
        VehicleInfo[vehicleID][vColor2]             = cache_get_row_int( 0, 8, g_Handle );
        cache_get_row( 0, 1, VehicleInfo[vehicleID][vOwner], g_Handle );
       
        VehicleInfo[vehicleID][vLocked]             = cache_get_row_int( 0, 9, g_Handle );
        VehicleInfo[vehicleID][vID] = CreatePersVehicle( VehicleInfo[vehicleID][vModel],
                                                         VehicleInfo[vehicleID][vLoc][0], VehicleInfo[vehicleID][vLoc][1], VehicleInfo[vehicleID][vLoc][2], VehicleInfo[vehicleID][vLoc][3],
                                                         VehicleInfo[vehicleID][vColor1], VehicleInfo[vehicleID][vColor2], VehicleInfo[vehicleID][vOwner], VehicleInfo[vehicleID][vLocked], false );

        printf( "Vehicle from %s has been loaded. (ID: %i)", VehicleInfo[vehicleID][vOwner], vehicleID );
    }
    return 1;
}
Whenever i create a new vehicle in game, all of the data gets succesfully saved. Including the username, which in my case will be 'Wesley'. Whenever i try to load every vehicles, everything loads except the vOwner.
Reply
#17

The problem may be very, very simple. The code looks perfect, have you checked the VehicleInfo enum? Are you sure that "VehicleInfo[vehicleID][vOwner]" is an array?

Last thing that I can advice is,

pawn Код:
new test[24];
     cache_get_row( 0, 1, test, g_Handle );
     print(test);
If it works, try;

pawn Код:
new test[24];
     cache_get_row( 0, 1, test, g_Handle );
     format(VehicleInfo[vehicleID][vOwner], 24, "%s", test);
I cannot find any other solution, sorry.
Reply
#18

vOwner is in the enum, and is made a string as well. In the DB it is a varchar, with the same size as the variable in the enum.

Ive tried both options, and neither of them works. Somehow it just doesnt load in the variable.
Reply
#19

Ive just tried some new things, and now with a different command it does load the string in to a variable.

pawn Код:
CMD:showteles( playerid, params[] )
{
    new query[128];

    for( new i = 1; i < MAX_TELES; i ++ )
    {
        format( query, sizeof query, "SELECT teleportid, teleportcmd FROM teleports WHERE teleportid = %i", i );
        mysql_function_query( g_Handle, query, true, "ShowTeles", "i", playerid );
    }
    return 1;
}

forward ShowTeles( playerid );
public ShowTeles( playerid )
{
    new num, row, fetchid, fetchcmd[50], pString[128];
    cache_get_data( num, row, g_Handle );
    if( row )
    {
        fetchid = cache_get_row_int( 0, 0, g_Handle );
        cache_get_row( 0, 1, fetchcmd, g_Handle );

        format( pString, sizeof pString, "TeleportID: %i | TeleportCommand: /%s", fetchid, fetchcmd );
        SendClientMessage( playerid, -1, pString );
    }
    return 1;
}
Both codes are practically the same.
Reply
#20

Any error logs?
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)