Loading only the rows from the database which is in it
#1

Hey there, I'm creating an all in one gamemode, with housesystem, admin system, login, etc,etc.

But as you can see in the next code, it's looping ALL the vehicles, even if it does not exists.
So how can I make it that it only loads the rows that are in there, and if he doesn't find any more, it stops?


pawn Код:
// Loop all personal vehicles
stock LoopVehicles()
{
    new query[500], savestring[200], Cars[128];
    for(new vid = 1; vid < MAX_VEHICLES; vid++)
    {
            format(query, sizeof(query), "SELECT * FROM Vehicles WHERE vID='%i'", vid);
            mysql_query(query);
            mysql_store_result();
            while(mysql_fetch_row_format(query, "|"))
            {
                mysql_fetch_field_row(savestring, "vID"); VehInfo[vid][vID] = strval(savestring);
                format(VehInfo[vid][Owner], MAX_PLAYER_NAME, "%s", mysql_fetch_field_row(savestring, "Owner"));
                //mysql_fetch_field_row(savestring, "Owner"); format(VehInfo[vid][Owner], 24, "%s",savestring);
                mysql_fetch_field_row(savestring, "SpawnX"); VehInfo[vid][SpawnX] = strval(savestring);
                mysql_fetch_field_row(savestring, "SpawnY"); VehInfo[vid][SpawnY] = strval(savestring);
                mysql_fetch_field_row(savestring, "SpawnZ"); VehInfo[vid][SpawnZ] = strval(savestring);
                mysql_fetch_field_row(savestring, "SpawnAngle"); VehInfo[vid][SpawnAngle] = strval(savestring);
                mysql_fetch_field_row(savestring, "Model"); VehInfo[vid][Model] = strval(savestring);
                mysql_fetch_field_row(savestring, "BelongsTo"); VehInfo[vid][BelongsToHouse] = strval(savestring);
                mysql_fetch_field_row(savestring, "Price"); VehInfo[vid][Price] = strval(savestring);
                mysql_fetch_field_row(savestring, "SellPrice"); VehInfo[vid][SellPrice] = strval(savestring);
                mysql_fetch_field_row(savestring, "color1"); VehInfo[vid][color1] = strval(savestring);
                mysql_fetch_field_row(savestring, "color2"); VehInfo[vid][color2] = strval(savestring);
            }
            mysql_free_result();
           
           
            CreateVehicle(VehInfo[vid][Model], VehInfo[vid][SpawnX], VehInfo[vid][SpawnY], VehInfo[vid][SpawnZ], VehInfo[vid][SpawnAngle], VehInfo[vid][color1], VehInfo[vid][color2], 200);
            format(Cars, sizeof(Cars), "{ff6600}%s owner:\n%s",GetVehicleFriendlyName(vid), VehInfo[vid][Owner]);
            VehLabel[vid] = Create3DTextLabel(Cars, 0xFFFFFFFF, 0.0, 0.0, 0.0, 50.0, -1, 0);
            Attach3DTextLabelToVehicle(VehLabel[vid], vid, 0.0, 0.0, 0.4);
           
    }
    return 1;
}
Reply
#2

Im not going to bump this twice.
Reply
#3

Under

pawn Код:
mysql_store_result();
check for

pawn Код:
if(mysql_num_rows() > 0)
Reply
#4

Remove the for-loop alltogether and remove the where clause from your query. Increment vid inside the while loop instead (after all the assignments).
Reply
#5

How do you mean with I should increment the 'vid' into the while loop?
Reply
#6

Quote:
Originally Posted by SomebodyAndMe
Посмотреть сообщение
How do you mean with I should increment the 'vid' into the while loop?
Anyone?
Reply
#7

Even if I tell you EXACTLY what to do, you're unable to do it ...

pawn Код:
// Loop all personal vehicles
stock LoopVehicles()
{
    savestring[200], Cars[128];
    new vid; // idx
   
    mysql_query("SELECT * FROM Vehicles");
    mysql_store_result();

    while(mysql_fetch_row_format(query, "|"))
    {
        mysql_fetch_field_row(savestring, "vID"); VehInfo[vid][vID] = strval(savestring);
        format(VehInfo[vid][Owner], MAX_PLAYER_NAME, "%s", mysql_fetch_field_row(savestring, "Owner"));
        //mysql_fetch_field_row(savestring, "Owner"); format(VehInfo[vid][Owner], 24, "%s",savestring);
        mysql_fetch_field_row(savestring, "SpawnX"); VehInfo[vid][SpawnX] = strval(savestring);
        mysql_fetch_field_row(savestring, "SpawnY"); VehInfo[vid][SpawnY] = strval(savestring);
        mysql_fetch_field_row(savestring, "SpawnZ"); VehInfo[vid][SpawnZ] = strval(savestring);
        mysql_fetch_field_row(savestring, "SpawnAngle"); VehInfo[vid][SpawnAngle] = strval(savestring);
        mysql_fetch_field_row(savestring, "Model"); VehInfo[vid][Model] = strval(savestring);
        mysql_fetch_field_row(savestring, "BelongsTo"); VehInfo[vid][BelongsToHouse] = strval(savestring);
        mysql_fetch_field_row(savestring, "Price"); VehInfo[vid][Price] = strval(savestring);
        mysql_fetch_field_row(savestring, "SellPrice"); VehInfo[vid][SellPrice] = strval(savestring);
        mysql_fetch_field_row(savestring, "color1"); VehInfo[vid][color1] = strval(savestring);
        mysql_fetch_field_row(savestring, "color2"); VehInfo[vid][color2] = strval(savestring);

        CreateVehicle(VehInfo[vid][Model], VehInfo[vid][SpawnX], VehInfo[vid][SpawnY], VehInfo[vid][SpawnZ], VehInfo[vid][SpawnAngle], VehInfo[vid][color1], VehInfo[vid][color2], 200);
        format(Cars, sizeof(Cars), "{ff6600}%s owner:\n%s",GetVehicleFriendlyName(vid), VehInfo[vid][Owner]);
        VehLabel[vid] = Create3DTextLabel(Cars, 0xFFFFFFFF, 0.0, 0.0, 0.0, 50.0, -1, 0);
        Attach3DTextLabelToVehicle(VehLabel[vid], vid, 0.0, 0.0, 0.4);
        vid++; // HERE
    }
   
    mysql_free_result();
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)