21.02.2017, 20:44
Directly taken from my new gamemode:
This code uses much smaller queries.
The callback is called with every modification done to just one component, why update them all?
If you tune a vehicle completely, you would send 14 queries where every value is updated again 14 times in a row, where only 1 value changes.
Smaller queries = smaller problems = faster script = less stress on your MySQL server.
Also, place this code in your gamemode as well:
If an error occurs with a query, this code will print it directly to the server console.
No need to dig in a huge log-file to figure out if some query failed execution.
PHP код:
// This callback gets called whenever a player mods his vehicle
public OnVehicleMod(playerid, vehicleid, componentid)
{
// Setup local variables
new componenttype, query[100];
// Get the componenttype of the component
componenttype = GetVehicleComponentType(componentid);
// Let the player pay for applying the component to his vehicle
Player_GiveMoney(playerid, -AVehicleModPrices[componentid - 1000]);
// Store the component in the AVehicleData array
AVehicleData[vehicleid][Components][componenttype] = componentid;
// If the vehicletype is a house-vehicle, save the component to the database
if (AVehicleData[vehicleid][VehicleType] == VEHICLETYPE_HOUSE)
{
// Construct the query based on the componenttype
switch (componenttype)
{
case 0: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Spoiler = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][0], AVehicleData[vehicleid][SQLID]);
case 1: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Hood = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][1], AVehicleData[vehicleid][SQLID]);
case 2: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Roof = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][2], AVehicleData[vehicleid][SQLID]);
case 3: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Sideskirt = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][3], AVehicleData[vehicleid][SQLID]);
case 4: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Lamps = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][4], AVehicleData[vehicleid][SQLID]);
case 5: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Nitro = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][5], AVehicleData[vehicleid][SQLID]);
case 6: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Exhaust = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][6], AVehicleData[vehicleid][SQLID]);
case 7: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Wheels = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][7], AVehicleData[vehicleid][SQLID]);
case 8: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Stereo = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][8], AVehicleData[vehicleid][SQLID]);
case 9: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET Hydraulics = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][9], AVehicleData[vehicleid][SQLID]);
case 10: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET FrontBumper = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][10], AVehicleData[vehicleid][SQLID]);
case 11: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET RearBumper = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][11], AVehicleData[vehicleid][SQLID]);
case 12: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET VentRight = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][12], AVehicleData[vehicleid][SQLID]);
case 13: mysql_format(SQL_db, query, sizeof(query), "UPDATE housevehicles SET VentLeft = '%i' WHERE ID = '%i'", AVehicleData[vehicleid][Components][13], AVehicleData[vehicleid][SQLID]);
}
// Execute the query to save the applied component for the vehicle
mysql_tquery(SQL_db, query, "", "");
}
return 1;
}
The callback is called with every modification done to just one component, why update them all?
If you tune a vehicle completely, you would send 14 queries where every value is updated again 14 times in a row, where only 1 value changes.
Smaller queries = smaller problems = faster script = less stress on your MySQL server.
Also, place this code in your gamemode as well:
PHP код:
// This will print the query and all related data to the server's console in case there is an error, useful for debugging
public OnQueryError(errorid, const error[], const callback[], const query[], MySQL:handle)
{
printf(" ");
printf("[ERROR MYSQL]");
printf("Error id: %i", errorid);
printf("Error message: %s", error);
printf("Query: %s", query);
printf("Callback: %s", callback);
printf(" ");
return 1;
}
No need to dig in a huge log-file to figure out if some query failed execution.