'EasyData' (MySQL) Problem
#1

I'm wrong ?
PHP код:
    for(new iMAX_VEHICLESi++)
    {
        new 
DB::Fetch("Vehicle"___"`ID` = '%d'"i);
        
//printf("fetch; %i - %i - %i", fetch_rows_count(), i, a);
        
if (fetch_rows_count() > 0)
        {
            
vStats[i][vSQLIB] = fetch_row_id();
             
vStats[i][vID] = fetch_int("VehicleID");
             
vStats[i][vX] = fetch_float("x");
             
vStats[i][vY] = fetch_float("y");
             
vStats[i][vZ] = fetch_float("z");
             
vStats[i][vColor1] = fetch_int("Color1");
             
vStats[i][vColor2] = fetch_int("Color2");
             
vStats[i][vHealth] = fetch_float("Health");
             
vStats[i][vModel] = fetch_int("Model");
             
vStats[i][vRespawn] = fetch_int("Respawn");
             
vStats[i][vZr] = fetch_float("Angle");
             
fetch_string("Plate"vStats[i][vPlate], 20);
             new 
vehicleid CreateVehicle(vStats[vehicleid][vModel], vStats[vehicleid][vX], vStats[vehicleid][vY], vStats[vehicleid][vZ], vStats[vehicleid][vZr], vStats[vehicleid][vColor1], vStats[vehicleid][vColor2], vStats[vehicleid][vRespawn]);
            
SetVehicleHealth(vehicleidvStats[vehicleid][vHealth]);
            
SetVehicleNumberPlate(vehicleidvStats[vehicleid][vPlate]);
        }
    } 
This piece of code creating 50 vehicles with the same parameters and avoid player's data load correctly.
Reply
#2

UP.
Reply
#3

Something like this would work I suppose:
pawn Код:
new cache = DB::Fetch("Vehicle");
do
{
    // retrieve data and create vehicle..
}
while (fetch_next_row(cache));
The only thing I am unsure is fetch_row_id as I have never used that include before.
Reply
#4

I think you didn't read the reply in the thread:
http://forum.sa-mp.com/showpost.php?...&postcount=124

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Something like this would work I suppose:
pawn Код:
new cache = DB::Fetch("Vehicle");
do
{
    // retrieve data and create vehicle..
}
while (fetch_next_row(cache));
The only thing I am unsure is fetch_row_id as I have never used that include before.
Row id returns the primary key's value for that row.
Reply
#5

I didn't, sorry.
This is working but can you explain the code.. I mean, how this work?
Goddamn, MySQL is so hard to understand. >.<
Reply
#6

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
I didn't, sorry.
This is working but can you explain the code.. I mean, how this work?
Goddamn, MySQL is so hard to understand. >.<
Well DB::Fetch is an alternative (easy one) to SELECT queries and also you can perform SORT functions as well in it.

So DB::Fetch will load all the eligible rows. DB::Fetch also return a Cache id which can be useful when you want to perform more than one SELECT query i.e. more than one DB::Fetch at the same time.

So lets say we have 10 rows in table "Vehicles". According to this code:
pawn Код:
DB::Fetch("Vehicles");
All the 10 rows will be loaded.

So if you use fetch_row_id, it should return you "1" (primary keys starts as natural numbers but MySQL starts them as whole numbers).

So the first row is loaded and you can use the fetch_* functions to get its data. Now if you want to goto the next row, you can do so by fetch_next_row.
pawn Код:
DB::Fetch("Vehicles");

fetch_row_id(); // this is the 1st row
fetch_int("data");

fetch_next_row(); // skipped to next row
fetch_row_id(); // this will now return 2 i.e. 2nd row
fetch_int("data");

fetcher_close();
But its not good to individually load all rows. In this case we only have 10 rows but if you have upto something like MAX_VEHICLES, you'll be done with programming !

So as you done there, we'll similarly use loops (while - loop).
"Why we used do - while loop?" - Because if we directly do "while (fetch_next_row())", we will skip the first row every time.

pawn Код:
DB::Fetch("Vehicles", MAX_VEHICLES); // since we have a limit on vehicles
do
{
    // fetch data here
}
while (fetch_next_row());
fetcher_close();
Also, fetch_next_row returns a boolean, if its true that means next row is existing in the table and has been loaded, false means NO.
Reply
#7

Alright. I'll keep your post in bookmark. Thanks.
I seem barely but.. I'm trying to do my best. x)
This code doesn't work.
PHP код:
CMD:garer(playerid)
{
    if(
pAccount[playerid][pAdmin] < SADMIN) return ErrorMsg(playerid_false);

    new 
vehicleid GetPlayerVehicleID(playerid),
        
Float:x,
        
Float:y,
        
Float:z;

    if(
vehicleid == INVALID_VEHICLE_ID) return ErrorMsg(playerid"Vous devez кtre dans un vйhicule.");

    
GetVehiclePos(vehicleidxyz);
    
DB::Fetch("Vehicle"___"`ID` = '%d'"vehicleid);
    if (
fetch_rows_count() > 0)
    {
        
AdminMessage(playerid"Changement des stats du vйhicule ID: %i."vehicleid);
        
DB::Update("ID"vStats[vehicleid][vSQLIB], 3,
                    
"x"FLOATx,
                    
"y"FLOATy,
                    
"z"FLOATz);
    }
    else
    {
        
vStats[vehicleid][vSQLIB] = fetch_row_id();

        
DB::CreateRow("Vehicle",
            
"ID"INTEGERvStats[vehicleid][vSQLIB],
             
"Model"INTEGERGetVehicleModel(vehicleid),
             
"VehicleID"INTEGERvehicleid);

        
AdminMessage(playerid"Vйhicule ID:%i en cours de crйation ..."vehicleid);
        
AdminMessage(playerid"Enregistrement des donnйes ...");

        
vStats[vehicleid][vSQLIB] = fetch_row_id();
         
vStats[vehicleid][vID] = vehicleid;
         
vStats[vehicleid][vX] = x;
         
vStats[vehicleid][vY] = y;
         
vStats[vehicleid][vZ] = z;
         
vStats[vehicleid][vColor1] = random(150);
         
vStats[vehicleid][vColor2] = random(150);
         
vStats[vehicleid][vHealth] = 1500.0;
         
vStats[vehicleid][vModel] = GetVehicleModel(vehicleid);
         
vStats[vehicleid][vRespawn] = 1800;
         
format(vStats[vehicleid][vPlate], 10GetARandomPlate());

        
AdminMessage(playerid"Donnйes enregistrйes. Attributions des nouvelles donйes au vйhicule.");

         
ChangeVehicleColor(vehicleidvStats[vehicleid][vColor1], vStats[vehicleid][vColor2]);
         
RepairVehicle(vehicleid);
         
SetVehicleHealth(vehicleidvStats[vehicleid][vHealth]);
         
SaveVehicleStats(vehicleid);

        
AdminMessage(playerid"Attributions terminйes.");
    }
    
fetcher_close();
    return 
1;

If you need english translate, say it. Sorry. '-'
Reply
#8

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
Alright. I'll keep your post in bookmark. Thanks.
I seem barely but.. I'm trying to do my best. x)
This code doesn't work.
PHP код:
CMD:garer(playerid)
{
    if(
pAccount[playerid][pAdmin] < SADMIN) return ErrorMsg(playerid_false);
    new 
vehicleid GetPlayerVehicleID(playerid),
        
Float:x,
        
Float:y,
        
Float:z;
    if(
vehicleid == INVALID_VEHICLE_ID) return ErrorMsg(playerid"Vous devez кtre dans un vйhicule.");
    
GetVehiclePos(vehicleidxyz);
    
DB::Fetch("Vehicle"___"`ID` = '%d'"vehicleid);
    if (
fetch_rows_count() > 0)
    {
        
AdminMessage(playerid"Changement des stats du vйhicule ID: %i."vehicleid);
        
DB::Update("ID"vStats[vehicleid][vSQLIB], 3,
                    
"x"FLOATx,
                    
"y"FLOATy,
                    
"z"FLOATz);
    }
    else
    {
        
vStats[vehicleid][vSQLIB] = fetch_row_id();
        
DB::CreateRow("Vehicle",
            
"ID"INTEGERvStats[vehicleid][vSQLIB],
             
"Model"INTEGERGetVehicleModel(vehicleid),
             
"VehicleID"INTEGERvehicleid);
        
AdminMessage(playerid"Vйhicule ID:%i en cours de crйation ..."vehicleid);
        
AdminMessage(playerid"Enregistrement des donnйes ...");
        
vStats[vehicleid][vSQLIB] = fetch_row_id();
         
vStats[vehicleid][vID] = vehicleid;
         
vStats[vehicleid][vX] = x;
         
vStats[vehicleid][vY] = y;
         
vStats[vehicleid][vZ] = z;
         
vStats[vehicleid][vColor1] = random(150);
         
vStats[vehicleid][vColor2] = random(150);
         
vStats[vehicleid][vHealth] = 1500.0;
         
vStats[vehicleid][vModel] = GetVehicleModel(vehicleid);
         
vStats[vehicleid][vRespawn] = 1800;
         
format(vStats[vehicleid][vPlate], 10GetARandomPlate());
        
AdminMessage(playerid"Donnйes enregistrйes. Attributions des nouvelles donйes au vйhicule.");
         
ChangeVehicleColor(vehicleidvStats[vehicleid][vColor1], vStats[vehicleid][vColor2]);
         
RepairVehicle(vehicleid);
         
SetVehicleHealth(vehicleidvStats[vehicleid][vHealth]);
         
SaveVehicleStats(vehicleid);
        
AdminMessage(playerid"Attributions terminйes.");
    }
    
fetcher_close();
    return 
1;

If you need english translate, say it. Sorry. '-'
Since SAMP has a limit on vehicles, you cannot use vehicle ids as array's indexes as long as you don't keep the vehicle when the owner disconnects.

Anyways, here is how you should detect the ID:
PHP код:
GetVehicleArrayID(vehicleid)
{
    for (new 
iMAX_VEHICLESi++)
    {
        if (
vStats[i][vID] == vehiclid)
            return 
i;
    }
    return -
1;

So you have to use this function to get a vehicle's index in an array every time you want to update the data.

PHP код:
CMD:garer(playerid)
{
    if(
pAccount[playerid][pAdmin] < SADMIN) return ErrorMsg(playerid_false);
    new 
vehicleid GetPlayerVehicleID(playerid);
    if(
vehicleid == INVALID_VEHICLE_ID) return ErrorMsg(playerid"Vous devez кtre dans un vйhicule.");
    new 
FloatxFloatyFloatz;
    
GetVehiclePos(vehicleidxyz);
    new 
idx GetVehicleArrayID(vehicleid);
    if (
idx != -1)
    {
        
AdminMessage(playerid"Changement des stats du vйhicule ID: %i."vehicleid);
        
DB::Update("Vehicle"vStats[idx][vSQLIB], 1,
                    
"x"FLOATx,
                    
"y"FLOATy,
                    
"z"FLOATz);
        return 
1;
    }
    
    for (new 
iMAX_VEHICLESi++)
    {
        if (
vStats[vehicleid][vSQLIB] == 0)
        {
            
idx i;
            break;
        }
    }
    if (
idx == -1)
    {
        
// for some reason the array isn't empty
        // make sure you set the "vSQLIB" to 0 when the owner disconnects or vehicle is destroyed
        
return 1;
    }
    
AdminMessage(playerid"Vйhicule ID:%i en cours de crйation ..."vehicleid);
    
AdminMessage(playerid"Enregistrement des donnйes ...");
    
vStats[idx][vID] = vehicleid;
    
vStats[idx][vX] = x;
    
vStats[idx][vY] = y;
    
vStats[idx][vZ] = z;
    
vStats[idx][vColor1] = random(150);
    
vStats[idx][vColor2] = random(150);
    
vStats[idx][vHealth] = 1500.0;
    
vStats[idx][vModel] = GetVehicleModel(vehicleid);
    
vStats[idx][vRespawn] = 1800;
    
format(vStats[idx][vPlate], 10GetARandomPlate());
    
DB::CreateRow("Vehicle",
        
"x"FLOATvStats[idx][vX],
        
"y"FLOATvStats[idx][vY],
        
"z"FLOATvStats[idx][vZ],
        
"Angle"FLOAT90.0,
        
"color1"INTEGERvStats[idx][vColor1],
        
"color2"INTEGERvStats[idx][vColor2],
        
"Health"FLOATvStats[idx][vHealth],
        
"Model"INTEGERvStats[idx][vModel],
        
"Plate"STRINGvStats[idx][vPlate]
    );
    
DB::Fetch("Vehicle"1__"`x` = %f, `y` = %f, `z` = %f"xyz);
    
vStats[idx][vSQLIB] = fetch_row_id();
    
fetcher_close();
    
AdminMessage(playerid"Donnйes enregistrйes. Attributions des nouvelles donйes au vйhicule.");
    
ChangeVehicleColor(vehicleidvStats[vehicleid][vColor1], vStats[vehicleid][vColor2]);
    
RepairVehicle(vehicleid);
    
SetVehicleHealth(vehicleidvStats[vehicleid][vHealth]);
    
SaveVehicleStats(vehicleid);
    
AdminMessage(playerid"Attributions terminйes.");
    return 
1;

Reply
#9

What do you mean by 'Vehicle's index' ? Why don't you take the ID of a vehicle ?
I don't understand this one:
Код:
Since SAMP has a limit on vehicles, you cannot use vehicle ids as array's indexes as long as you don't keep the vehicle when the owner disconnects.
PHP код:
DB::Fetch("Vehicle"1__"`x` = %f, `y` = %f, `z` = %f"xyz); 
Why don't you take the vehicle's ID?
Thanks you so much for the code and for your spent time to explain me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)