Let's take the example from the wiki:
pawn Код:
#define THREAD_LOADPLAYER (5) //You can put 0, 1, 2, 3.. to identify the sent query. It works as a "query ID" :)
public OnPlayerConnect(playerid)
{
mysql_query("SELECT * FROM players WHERE name='Myname'",THREAD_LOADPLAYER,playerid); //OK, here, you define the query ID with THREAD_LOADPLAYER (which is 5).
//Also you can do that without replacing 5 with THREAD_LOADPLAYERS, just use 5 or another number,
//but don't repeat them in other queries because they works as IDs to identify it in OnQueryFinish.
//If you mix them they'll execute wrong codes. Exactly as dialogs.
return 1;
}
public OnQueryFinish(query[], resultid, extraid, connectionHandle)
{
switch(resultid) //This is the ID which you specificated in mysql_query (in OnPlayerConnect), thanks this you can identify which query has finished.
{
case THREAD_LOADPLAYER: //This works as in dialogs, it's as a dialogid. (You can use 5 there instead THREAD_LOADPLAYER)
{
mysql_store_result(); //Stores what you specificated in mysql_query, in this case selects MyName from the table players.
if(IsPlayerConnected(extraid)) //extraid is the value which you sent with mysql_query as extra. in this case playerid,
//and it checks if the player is still connected to execute the code.
{
//Execute your code
//As it says, execute your code. For example? Load player data. money, score, etc.
}
mysql_free_result(); //free the query.
}
}
return 1;
}
/* Another example */
#define QUERY_ONE (1)
public OnPlayerDeath(playerid)
{
Deaths[playerid]++;
new name[24], query[128];
GetPlayerName(playerid,name,24);
format(query,128,"SELECT * FROM players WHERE username='%s'",name); //Selecting player's name from the table 'players'.
mysql_query(query,QUERY_ONE,playerid); //Define query's id giving it QUERY_ONE's value and we send playerid as extraid.
return 1;
}
public OnQueryFinish(query[], resultid, extraid, connectionHandle)
{
switch(resultid)
{
case QUERY_ONE:
{
mysql_store_result();
if(IsPlayerConnected(extraid))
{
new update[128], name[24];
GetPlayerName(extraid,name,24);
format(update,128,"UPDATE players SET deaths=%d WHERE username='%s'",Deaths[extraid],name);
mysql_query(update);
}
mysql_free_result();
}
}
return 1;
}
Summarizing, these are called threaded scripts, for example you execute one query, but you won't know if it was executed 100% correctly. And with, OnQueryFinish you can. How? When the query finishes it calls OnQueryFinish, it was executed well, and now depending of the resultid which you inserted in mysql_query you execute the code which you want. For example, update something in the database.
I'm sorry for my English, it's not good enough to explain everything 100% well. I hope you understand, if not, just ask me.
Best regards!