SA-MP Forums Archive
Need help with mysql update... - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Need help with mysql update... (/showthread.php?tid=489982)



Need help with mysql update... - Scrillex - 24.01.2014

So basically with mysql I'm on we. It ain't my scripting language.. But still want to learn it.. maybe some one could
explain how to upgrade this code... I'm using MySQL plugin R34...

Here is my code:

pawn Код:
forward LoadGarages();
public LoadGarages()
{
    new arrCoords[15][64];
    new sql[80], row[512];
    format(sql, sizeof(sql), "SELECT COUNT(*) FROM Garages");
    mysql_query(sql);
    mysql_store_result();
    mysql_fetch_row(row);
    mysql_free_result();
    for (new idx=0; idx<MAX_GARAGES; idx++) {
        format(sql, sizeof(sql), "SELECT * FROM Garages WHERE GarageID=%d", idx);
        mysql_query(sql);
        mysql_store_result();
        if (mysql_num_rows() > 0) {
            mysql_fetch_row(row);
            split(row, arrCoords, '|');
            mysql_free_result();
            Garages[idx][EnterX] = floatstr(arrCoords[1]);
            Garages[idx][EnterY] = floatstr(arrCoords[2]);
            Garages[idx][EnterZ] = floatstr(arrCoords[3]);
            Garages[idx][ExitX] = floatstr(arrCoords[4]);
            Garages[idx][ExitY] = floatstr(arrCoords[5]);
            Garages[idx][ExitZ] = floatstr(arrCoords[6]);
            Garages[idx][EnterAngle] = floatstr(arrCoords[7]);
            Garages[idx][ExitAngle] = floatstr(arrCoords[8]);
            Garages[idx][Owned] = strval(arrCoords[9]);
            strmid(Garages[idx][Owner], arrCoords[10], 0, strlen(arrCoords[10]), 255);
            Garages[idx][Price] = strval(arrCoords[11]);
            Garages[idx][ExitInterior] = strval(arrCoords[12]);
            Garages[idx][Dynamic] = strval(arrCoords[13]);
            Garages[idx][Locked] = strval(arrCoords[14]);

            new string[128];
            if(Garages[idx][Owned] == 0)
            {
                new garagelocation[MAX_ZONE_NAME];
                GetCoords2DZone(Garages[idx][EnterX],Garages[idx][EnterY], garagelocation, MAX_ZONE_NAME);
                format(string, sizeof(string), "[Garage For Sale]\nAddress: %d %s\nPrice: $%d",idx,garagelocation,Garages[idx][Price]);
                garagetext[idx] = Create3DTextLabel(string,0xbec339ff,Garages[idx][EnterX],Garages[idx][EnterY],Garages[idx][EnterZ],5.0,0, 0);
            } else {
                new garagelocation[MAX_ZONE_NAME];
                GetCoords2DZone(Garages[idx][EnterX],Garages[idx][EnterY], garagelocation, MAX_ZONE_NAME);
                format(string, sizeof(string), "[Garage]\nAddress: %d %s\nOwner: %s",idx,garagelocation,Garages[idx][Owner]);
                garagetext[idx] = Create3DTextLabel(string,0xbec339ff,Garages[idx][EnterX],Garages[idx][EnterY],Garages[idx][EnterZ],5.0,0, 0);
            }
        }
    }
    mysql_free_result();
    printf("%d Garages loaded from database", MAX_GARAGES);
    return true;
}

forward SaveGarages(busid);
public SaveGarages(busid)
{
    new query[1024];
    format(query, sizeof(query), "UPDATE Garages SET EnterX=%f,EnterY=%f,EnterZ=%f,ExitX=%f,ExitY=%f,ExitZ=%f,EnterAngle=%f,ExitAngle=%f WHERE GarageID=%d",
    Garages[busid][EnterX],
    Garages[busid][EnterY],
    Garages[busid][EnterZ],
    Garages[busid][ExitX],
    Garages[busid][ExitY],
    Garages[busid][ExitZ],
    Garages[busid][EnterAngle],
    Garages[busid][ExitAngle],
    busid);
    mysql_query(query);
    //printf("Query : %s",query);

    format(query, sizeof(query), "UPDATE Garages SET Owned=%d,Owner='%s',Price=%d,Interior=%d,Dynamic=%d,Open=%d WHERE GarageID=%d",
    Garages[busid][Owned],
    Garages[busid][Owner],
    Garages[busid][Price],
    Garages[busid][ExitInterior],
    Garages[busid][Dynamic],
    Garages[busid][Locked],
    busid);
    mysql_query(query);
    //printf("Query : %s",query);
}
With best regards Scrillex.. And btw big thanks for your help any answers will be appreciated.


Re: Need help with mysql update... - Shetch - 24.01.2014

This is a great tutorial I learned from.
https://sampforum.blast.hk/showthread.php?tid=337810


Re: Need help with mysql update... - Scrillex - 24.01.2014

In this case I would need to use : mysql_function_query(dbHandle, "SELECT COUNT(*) FROM Garage Or how it would work...


Re: Need help with mysql update... - Scrillex - 25.01.2014

BUMP.. Need some help..


Re: Need help with mysql update... - maddinat0r - 25.01.2014

Код:
stock LoadGarages()
{
	mysql_tquery(sql_handle, "SELECT * FROM Garages", "OnGarageDataLoad", "");
}

forward OnGarageDataLoad();
public OnGarageDataLoad()
{
    for(new i=0, max = cache_get_row_count(); i < max; ++i)
	{
		Garages[i][EnterX] = cache_get_row_float(i, 1);
		Garages[i][EnterY] = cache_get_row_float(i, 2);
		Garages[i][EnterZ] = cache_get_row_float(i, 3);
		//....
		cache_get_row(i, 10, Garages[i][Owner], sql_handle, MAX_PLAYER_NAME);
		Garages[i][Price] = cache_get_row_int(i, 11);
		//....
		
		//if(Garages[i][Owned]...
	}
	printf("%d Garages loaded from database", cache_get_row_count());
	return 1;
}
For that SaveGarages code, just replace "mysql_query" with "mysql_tquery" (like in function "LoadGarages").


Re: Need help with mysql update... - Scrillex - 25.01.2014

Like this? It will look like?
pawn Код:
stock LoadGarages()
{
    mysql_tquery(sql_handle, "SELECT * FROM Garages", "OnGarageDataLoad", "");
}

forward OnGarageDataLoad();
public OnGarageDataLoad()
{
    for(new i=0, max = cache_get_row_count(); i < max; ++i)
    {
        Garages[i][EnterX] = cache_get_row_float(i, 1);
        Garages[i][EnterY] = cache_get_row_float(i, 2);
        Garages[i][EnterZ] = cache_get_row_float(i, 3);
        Garages[idx][ExitX]  = cache_get_row_float(i, 4);
        Garages[idx][ExitY]  = cache_get_row_float(i, 5);
        Garages[idx][ExitZ]  = cache_get_row_float(i, 6);
        Garages[idx][EnterAngle]  = cache_get_row_float(i, 7);
        Garages[idx][ExitAngle]  = cache_get_row_float(i, 8);
        cache_get_row(i, 10, Garages[i][Owner], sql_handle, MAX_PLAYER_NAME);
        Garages[i][Price] = cache_get_row_int(i, 11);
        Garages[idx][ExitInterior] = cache_get_row(i, 12);
        Garages[idx][Dynamic] = cache_get_row(i, 13);
        Garages[idx][Locked] = cache_get_row(i, 14);
    }
    printf("%d Garages loaded from database", cache_get_row_count());
    return 1;
}
Thank you for your time.. I really appreciate it..


Re: Need help with mysql update... - maddinat0r - 25.01.2014

cache_get_row is for strings (like the Owner-variable), cache_get_row_float is for float values (like EnterX, ExitX, ...) and cache_get_row_int is for int values (like Price, Dynamic, Locked, ...). That means you have the last three variables assigned wrong (ExitInterior, Dynamic and Locked).