Load Mysql cars
#1

I'm posting this for my bro, cause he dont have an acc.

Hey, in my old topic i asked u how to save a car -> worked
but now i want to load it and i got some problems.

Код:
stock LoadPlayerCars()
{
    new i;
        new string[6];
    for(; i<= //what here?; i++)
	{
		format(string, sizeof(string), "%d", i);
       format(Playercar[i][owner], MAX_PLAYER_NAME, mysql_GetString("cars", "owner", "id", string));
        Playercar[i][spawnx] = mysql_GetFloat("cars", "spawnx", "id", string);
        Playercar[i][spawny] = mysql_GetFloat("cars", "spawny", "id", string);
        Playercar[i][spawnz] = mysql_GetFloat("cars", "spawnz", "id", string);
        Playercar[i][spawnr] = mysql_GetFloat("cars", "spawnr", "id", string);
        Playercar[i][c1] = mysql_GetInt("cars", "c1", "id", string);
        Playercar[i][c2] = mysql_GetInt("cars", "c2", "id", string);
        format(Playercar[i][plate], 20, "%s", mysql_GetString("cars", "plate", "id", string));
        Playercar[i][modelid] = mysql_GetInt("Cars", "model", "id", string);
        Playercar[i][price] = mysql_GetInt("Cars", "price", "id", string);
        Playercar[i][car] = AddStaticVehicleEx(Playercar[i][modelid], Playercar[i][spawnx], Playercar[i][spawny], Playercar[i][spawnz], Playercar[i][spawnr], Playercar[i][c1], Playercar[i][c2], -1);
        
        printf("i = %d, owner = %s, x = %f, y = %f, z = %f, r = %f, c1 = %d, c2 = %d, plate = %s, model = %d, price = %d, car = %d", i, Playercar[i][modelid], Playercar[i][spawnx], Playercar[i][spawny], Playercar[i][spawnz], Playercar[i][spawnr], Playercar[i][c1], Playercar[i][c2], Playercar[i][plate], Playercar[i][modelid], Playercar[i][price], Playercar[i][car]);
How can i load them? And do u have a good mysql tutorial? Cause i dont get it well.

(sry for bad engl. )
Reply
#2

someone help me?
Reply
#3

Do not send a separate query for each vehicle (or house, business, etc.). This puts an unnecessary strain on the MySQL server. It's probably slower as well. I'm assuming that the GetFloat and GetInt wrappers also send seperate queries as well, which makes it all the more worse.

See here: https://sampforum.blast.hk/showthread.php?tid=366019
Reply
#4

Can you show your query as well?
I assume you used something like "SELECT * FROM housecars WHERE Owner = 'yourname'" and you've used mysql_query (without threading)?
Reply
#5

thanks for the tutorial link @Vince:

my source right now
Код:
stock LoadPlayerCars()
{

    mysql_query_callback(1, "SELECT * FROM cars",  "OnPlayerCarsLoaded"); // we won't pass any other variables to the callback
}
forward OnPlayerCarsLoaded();
public OnPlayerCarsLoaded()
{
    new
        idx,
        result[256],
        query[128];
	new downer,dplate,Float:sx,Float:sy,Float:sz,model,colo1,colo2,dprice,dcar,iD,Float:sr;
    format(result, 128, "SELECT * FROM `cars` WHERE `ID` = '%d'", id);
	mysql_query(query);
    mysql_store_result();

    // Here we start the while loop to fetch the rows
    while(mysql_fetch_row_format(result, "|"))
    {
        sscanf(result, "e<p<|>{i}is[16]s[24]ffffiiii>",iD,downer,dplate,sx,sy,sz,sr,model,colo1,colo2,dprice);
		Playercar[idx][owner] = downer;
		Playercar[idx][plate] = dplate;
		Playercar[idx][spawnx] = sx;
		Playercar[idx][spawny] = sy;
		Playercar[idx][spawnz] = sz;
		Playercar[idx][spawnr] = sr;
		Playercar[idx][modelid] = model;
		Playercar[idx][c1] = colo1;
		Playercar[idx][c2] = colo2;
		Playercar[idx][price] = dprice;
        Playercar[idx][car] = AddStaticVehicleEx(Playercar[idx][modelid],Playercar[idx][spawnx],Playercar[idx][spawny],Playercar[idx][spawnz],Playercar[idx][spawnr],Playercar[idx][c1],Playercar[idx][c2],60);
        idx++; // Now increment our index!
    }

    mysql_free_result();
    printf("[mysql] Succesful. Fetched %i rows from the database.", idx); //
    return 1;
}
It fetched 3 rows, thats okay cause i 've got 3 in my DB.
But my car doesnt appear. Did i do it correctly? Or is sth wrong?

#edit
i printed it.
printf(" ||CAR: %s %i %f %f %f %f %i %i %i", downer,modelid,sx,sy,sz,sr,colo1,colo2,price);
the output is
Код:
||CAR:  49 0.000000 0.000000 0.000000 0.000000 0 0 52
Wheres the mistake?
Reply
#6

You have ALOT of mistakes.

At first, you do a query to get all data from MySQL.
Then you run another query to only select a few cars from MySQL in the callback.

Then, you create a variable "id" but has value "0" by default, which you use as id in your query.
You'll ALWAYS get the cars with id 0, no matter which player's cars you want to load.

I assume owner and plate are strings, as seeing it in your sscanf command?
But you use them as if they were integers.
Reply
#7

wow. okay. Didnt see those mistakes.
Well, i have to delete the
pawn Код:
format(result, 128, "SELECT * FROM `cars` WHERE `ID` = '%d'", id);
?
Reply
#8

pawn Код:
// Somewhere in your code
LoadPlayerCars(PlayerData[playerid][SQL_ID]);

stock LoadPlayerCars(ID)
{
    new Query[128];

    mysql_format(1, Query, sizeof(Query), "SELECT * FROM `cars` WHERE `ID` = '%d'", ID);
    mysql_tquery(1, Query, "OnPlayerCarsLoaded", ""); // we won't pass any other variables to the callback
}

forward OnPlayerCarsLoaded();
public OnPlayerCarsLoaded()
{
    // Get the amount of rows (cameras)
    new Rows = cache_get_row_count(1);

    // If there are any rows loaded, load data and create the cars
    if (Rows >= 1)
    {
        // Loop through all rows
        for (new idx; idx < Rows; idx++)
        {
            // Load the data
            cache_get_field_content(idx, "Owner", Playercar[idx][owner], 1, 24);
            cache_get_field_content(idx, "Plate", Playercar[idx][plate], 1, 16);
            Playercar[idx][spawnx] = cache_get_field_content_float(idx, "X", 1);
            Playercar[idx][spawny] = cache_get_field_content_float(idx, "Y", 1);
            Playercar[idx][spawnz] = cache_get_field_content_float(idx, "Z", 1);
            Playercar[idx][spawnr] = cache_get_field_content_float(idx, "Angle", 1);
            Playercar[idx][modelid] = cache_get_field_content_int(idx, "Model", 1);
            Playercar[idx][c1] = cache_get_field_content_int(idx, "Color1", 1);
            Playercar[idx][c2] = cache_get_field_content_int(idx, "Color2", 1);
            Playercar[idx][price] = cache_get_field_content_int(idx, "Price", 1);
            Playercar[idx][car] = CreateVehicle(Playercar[idx][modelid], Playercar[idx][spawnx], Playercar[idx][spawny], Playercar[idx][spawnz], Playercar[idx][spawnr], Playercar[idx][c1], Playercar[idx][c2], 60);
        }
    }

    printf("[mysql] Succesful. Fetched %i rows from the database.", Rows); //

    return 1;
}
Try this code.

I assume your columns have these names:
- ID
- Owner
- Plate
- X
- Y
- Z
- Angle
- Model
- Color1
- Color2
- Price

Adjust them accordingly.
Reply
#9

which version do u use? cause i don't have some of this codes
Reply
#10

I'm using plugin R34 by BlueG.
https://sampforum.blast.hk/showthread.php?tid=56564

These are threaded queries, your script doesn't need to wait for MySQL to load everything.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)