30.05.2011, 13:58
If you think about the SA-MP dialog system, when you use ShowPlayerDialog() and a player actions the dialog, the callback OnDialogResponse is called with the data that the user entered or selected (via listitem or inputtext).
With MySQL query threading, it's pretty simple - you have two callbacks (OnQueryFinish and OnQueryError) which is called when one of your queries completes, when you create a query with G-StyleZzZ's plugin, you are given the following parameters for executing a query with the mysql_query function:
The resultid is what the thread will send back to OnQueryFinish or OnQueryError, every time you execute a query, one of those callbacks are executed. Here's an example of how you'd track a query via its thread:
If you see in the command, I execute the query to retrieve data from every row for `accounts`, with the 'resultid' (thread) of THREAD_ACCOUNTS, then in the OnQueryFinish callback, I use 'extraid' (when I wrote the query line, I included 'playerid' for the 'extraid' parameter so we know what the playerid that should relate to that query, then in the OnQueryFinish callback, I check to see if 'resultid' matches THREAD_ACCOUNTS (1), and if it does then it finishes the job of the command.
Multi-threading is useful because SA-MP is single-threaded, while the MySQL plugin isn't - if you send multiple queries to multiple threads, SA-MP will continue processing and MySQL will process the queries on the other threads, so hence why people claim that multi-threading is more efficient. This is inexplicably useful if you're sending multiple queries to your MySQL server that may be slightly intensive. (like a really long query).
Sorry, I wrote more than I expected.
With MySQL query threading, it's pretty simple - you have two callbacks (OnQueryFinish and OnQueryError) which is called when one of your queries completes, when you create a query with G-StyleZzZ's plugin, you are given the following parameters for executing a query with the mysql_query function:
pawn Code:
mysql_query(query[], resultid, extraid, connectionHandle)
pawn Code:
#define THREAD_ACCOUNTS 1 // Define 'THREAD_ACCOUNTS' as 1, but you can just use 1, but this way you remember what your query thread IDs are so you don't lose track and declare multiple queries as the same ID
public OnQueryFinish(query[], resultid, extraid, connectionHandle) {
switch(resultid) { // Personal preference: switch > if, no advantages in PAWN IIRC
case THREAD_ACCOUNTS: {
mysql_store_result(); // Store our result so we can access mysql_num_rows()
new
szMessage[26]; // Create a string so we can format a message
format(szMessage, sizeof(szMessage), "There are %d accounts.", mysql_num_rows());
// mysql_num_rows is used to count how many rows the query picks up based on the criteria of your query
SendClientMessage(extraid, 0, szMessage); // Forward a message to the client
mysql_free_result(); // Free the result from memory because we no longer need it
}
}
}
CMD:allaccounts(playerid, params[]) {
// Remember, syntax: mysql_query(query[], resultid, extraid, connectionHandle)
mysql_query("SELECT * FROM `accounts`", THREAD_ACCOUNTS, playerid);
return 1;
}
Multi-threading is useful because SA-MP is single-threaded, while the MySQL plugin isn't - if you send multiple queries to multiple threads, SA-MP will continue processing and MySQL will process the queries on the other threads, so hence why people claim that multi-threading is more efficient. This is inexplicably useful if you're sending multiple queries to your MySQL server that may be slightly intensive. (like a really long query).
Sorry, I wrote more than I expected.