MySQL string to variable
#21

Arent any errors, just that the query gets excecuted corectly.
In the compiler no warnings or errors
Reply
#22

pawn Код:
new temp[MAX_PLAYER_NAME+2];
cache_get_row(0, 1, temp); format(VehicleInfo[vehicleID][vOwner],sizeof(temp),temp);
Are you trying to load all vehicles from the database ? If yes,then why not a query with select * from table and then a loop ?
Reply
#23

Im using a loop to load every vehicle, by calling the function in the loop

Anyways, ive tried to make the variable larger, didnt work. Tried to make it to 24, 25, 26 and 50; neither one of those worked
Reply
#24

Quote:
Originally Posted by Wesley221
Посмотреть сообщение
Im using a loop to load every vehicle, by calling the function in the loop

Anyways, ive tried to make the variable larger, didnt work. Tried to make it to 24, 25, 26 and 50; neither one of those worked
There is one simple way to loop through all the data..i don't know if it's faster or slower.. but i use it.
That's how i load my stuff..and it all loads perfectly.

pawn Код:
new TotalThings;

mysql_function_query(g_Handle, "SELECT * FROM table", true, "Loadthings","");

forward Loadthings();
public Loadthings() {
    new rows,fields,temp[64];
    cache_get_data(rows,fields);
    while(TotalThings < rows) {
        cache_get_row(TotalThings, 0, temp), eNum[TotalThings][eID] = strval(temp);
        cache_get_row(TotalThings, 1, temp), format(eNum[TotalThings][eName],sizeof(temp),temp);
        TotalThings++;
    }
}
If you are using r18 or whatever.. maybe there's a function that retrieves the string directly.. idk. haven't checked. i use r14 and i usually use cache_get_row for everything.

//Are you sure idx 1 is the owner name ?
// 0 - id
// 1 - owner
// 2 - etc.
Reply
#25

Thus far, ive tried 2 things to load the string.

pawn Код:
new fetch[24 + 2];
cache_get_field_content( 0, "username", fetch, g_Handle, sizeof fetch );

cache_get_row(0 , 1, fetch, g_Handle );
strcat( VehicleInfo[vehicleID][vOwner], fetch ); // VehicleInfo[vehicleID][Owner] is empty by default, so no need for strdel in this case
Both of them didnt load the strings.
Things that i know what couldnt be the case:
the field 'username' in the database is a VARCHAR, with the size of 24.
the field 'username' is in the database at place 1; vehicleid, username, modelid, etc
the variable 'vOwner' in the enum has the size MAX_PLAYER_NAME, which is 24.

When the gamemode starts, im calling the function 'VehicleLoadAll()', which is:
pawn Код:
stock VehicleLoadAll()
{
    for( new i = 1; i < MAX_PERS_VEHICLES; i ++ )
    {
        VehicleLoad( i );
    }
}
VehicleLoad():
pawn Код:
stock VehicleLoad( vehicleID )
{
    new query[128];
    format( query, sizeof query, "SELECT * FROM vehicles WHERE carid = %i", vehicleID );
    mysql_function_query( g_Handle, query, true, "OnVehicleLoad", "i", vehicleID );
    return 1;
}
OnVehicleLoad:
pawn Код:
forward OnVehicleLoad( vehicleID );
public OnVehicleLoad( vehicleID )
{
    new row, num;
    cache_get_data( row, num, g_Handle );
    if( row )
    {
        new fetch[24 + 2];
        cache_get_field_content( 0, "username", fetch, g_Handle, sizeof fetch );
        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 );
       
        strcat( VehicleInfo[vehicleID][vOwner], fetch );
       
        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;
}
And this query to create the table:
pawn Код:
strcat( query, "CREATE TABLE IF NOT EXISTS vehicles( carid INT( 11 ) AUTO_INCREMENT, username VARCHAR( 24 ), modelid INT( 11 ), " );
    strcat( query, "xpos FLOAT, ypos FLOAT, zpos FLOAT, apos FLOAT, color1 INT( 11 ), color2 INT( 11 ), locked TINYINT( 1 ), " );
    strcat( query, "compslot0 INT( 4 ), compslot1 INT( 4 ), compslot2 INT( 4 ), compslot3 INT( 4 ), compslot4 INT( 4 ), compslot5 INT( 4 ), " );
    strcat( query, "compslot6 INT( 4 ), compslot7 INT( 4 ), compslot8 INT( 4 ), compslot9 INT( 4 ), compslot10 INT( 4 ), " );
    strcat( query, "compslot11 INT( 4 ), PRIMARY KEY( carid ) )" );
    mysql_function_query( g_Handle, query, false, "SendQuery", "" );
The variable size of 'query' is large enough, since the table gets created without any errors.
Reply
#26

Quote:
Originally Posted by Wesley221
Посмотреть сообщение
-
Well..this is weird. I have tried on my computer the same code..all works fine.

Is your vehicleinfo enum big enough ?

This is the code that i've tried.

pawn Код:
new row, num;
    cache_get_data( row, num, g_Handle );
    if( row )
    {
        new fetch[24 + 2];
        cache_get_field_content( 0, "username", fetch, g_Handle, sizeof fetch );
        printf("%s",fetch);
        format(VehicleInfo[vehicleID][vOwner],26,fetch);
        printf("%s",VehicleInfo[vehicleID][vOwner]);
        printf( "Vehicle from %s has been loaded. (ID: %i)", VehicleInfo[vehicleID][vOwner], vehicleID );
    }
Reply
#27

pawn Код:
enum carDataEnum
{
    vID,
    vOwner[MAX_PLAYER_NAME],
    vModel,
    Float:vLoc[4],
    vColor1,
    vColor2,
    vLocked
}
new VehicleInfo[MAX_PERS_VEHICLES][carDataEnum];
Thats the enum
Reply
#28

Ive just tried a few things, again. And I might know what is going wrong.
The code im using right now is:
pawn Код:
forward OnVehicleLoad( vehicleID );
public OnVehicleLoad( vehicleID )
{
    new row, num;
    cache_get_data( row, num, g_Handle );
    if( row )
    {
        new fetch[24 + 2];
        cache_get_field_content( 0, "username", fetch, g_Handle, sizeof fetch );
        printf( "|%s|", fetch );
        VehicleInfo[vehicleID][vModel]              = cache_get_row_int( 0, 2, g_Handle );
        VehicleInfo[vehicleID][vX]                  = cache_get_row_float( 0, 3, g_Handle );
        VehicleInfo[vehicleID][vY]                  = cache_get_row_float( 0, 4, g_Handle );
        VehicleInfo[vehicleID][vZ]                  = cache_get_row_float( 0, 5, g_Handle );
        VehicleInfo[vehicleID][vA]                  = 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 );
       
        strset( VehicleInfo[vehicleID][vOwner], fetch );
        printf( "|%s|", VehicleInfo[vehicleID][vOwner] );

        VehicleInfo[vehicleID][vLocked]             = cache_get_row_int( 0, 9, g_Handle );
        VehicleInfo[vehicleID][vID] = CreatePersVehicle( VehicleInfo[vehicleID][vModel],
                                                         VehicleInfo[vehicleID][vX], VehicleInfo[vehicleID][vY], VehicleInfo[vehicleID][vZ], VehicleInfo[vehicleID][vA],
                                                         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;
}
What i get from the prints is:
Код:
[16:13:46] |Wesley|
[16:13:46] |Wesley|
[16:13:46] Vehicle from  has been loaded. (ID: 1)
Which means, it gets the 'username' out of the database, which is 'Wesley'. It stores 'Wesley' in the variable fetch, which works aswell (first print). After that, im storing the value of 'fetch' in 'VehicleInfo[VehicleID][vOwner], which works aswell (second print). After that, im printing the last line, which is 'Vehicle from %s has been loaded.'. But somehow the data that was in VehicleInfo[vehicleID][vOwner] is gone now.

I dont have the slightest clue what is happening here, anyone can enlighten me?

Edit:
Im stupid

It wasn't in either one of those functions. It was in the CreatePersVehicle function.
pawn Код:
printf( "%i", vehicleid );
   
    VehicleInfo[vehicleid][vModel]  = vehicleModel;
    printf( "%i", VehicleInfo[vehicleid][vModel] );
   
    VehicleInfo[vehicleid][vX]      = xLoc;
    printf( "%f", VehicleInfo[vehicleid][vX] );
   
    VehicleInfo[vehicleid][vY]      = yLoc;
    printf( "%f", VehicleInfo[vehicleid][vY] );
   
    VehicleInfo[vehicleid][vZ]      = zLoc;
    printf( "%f", VehicleInfo[vehicleid][vZ] );
   
    VehicleInfo[vehicleid][vA]      = aLoc;
    printf( "%f", VehicleInfo[vehicleid][vA] );
   
    VehicleInfo[vehicleid][vColor1] = vehicleColor1;
    printf( "%i", VehicleInfo[vehicleid][vColor1] );
   
    VehicleInfo[vehicleid][vColor2] = vehicleColor2;
    printf( "%i", VehicleInfo[vehicleid][vColor2] );
   
    printf( "1:%s", VehicleInfo[vehicleid][vOwner] );
    printf( "vehicleowner: %s", vehicleOwner );
    format( VehicleInfo[vehicleid][vOwner], 24, vehicleOwner ); // <--
    printf( "2: %s", VehicleInfo[vehicleid][vOwner] );
I mixed some things up at the // <-- comment. Fixed now!
Thanks for the help anyhow
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)