SA-MP Forums Archive
converting unthreaded to threaded. - 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: converting unthreaded to threaded. (/showthread.php?tid=613731)



converting unthreaded to threaded. - DavidLuango - 31.07.2016

Hello guys!

I'm having problems trying to convert unthreaded queries to threaded queries.


Orginally in unthreaded I was simply asked to put the "LoadFunctions()" under OnGameModeInit. But after looking at tutorial I learned I can pass it under callback something like this.

THREADED QUEIRIES ATTEMPT:
PHP код:
public OnGameModeInit()
{
    
SetGameModeText("MySQL - Server");
    new 
query[400];
    for (new 
idid MAX_FACTIONSid++)
    {
         
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `Factions` WHERE `ID` = %d LIMIT 1"id);
        
mysql_tquery(mysqlquery"LoadFactions""");
    }
    
mysql_log(LOG_ALL);
    
mysql mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_DATABASEMYSQL_PASSWORD);
    if(
mysql_errno() != 0)
    {
        
printf("[MySQL] The connection has failed.");
    }
    else
    {
        
printf("[MySQL] The connection was successful.");
    }
    return 
true;
}
forward LoadFactions();
public 
LoadFactions()
{
    new 
rowsfields;
    
cache_get_data(rows,fields);
    if(
rows)
    {
        
cache_get_field_content_int(0"ID");
        
cache_get_field_content(0,"Name",Factions[Total_Factions_Created][Name]);
        
cache_get_field_content(0,"Rank1",Factions[Total_Factions_Created][Rank1]);
        
cache_get_field_content(0,"Rank2",Factions[Total_Factions_Created][Rank2]);
        
cache_get_field_content(0,"Rank3",Factions[Total_Factions_Created][Rank3]);
        
cache_get_field_content(0,"Rank4",Factions[Total_Factions_Created][Rank4]);
         
cache_get_field_content(0,"Rank5",Factions[Total_Factions_Created][Rank5]);
        
Total_Factions_Created++;
    }
    
printf("> %d factions have been loaded from the database."Total_Factions_Created); // Prints out the information of how many factions created
    
return 1;

UNTHREAD QUERIES
PHP код:
public OnGameModeInit()
{
    
LoadFactions();
}
stock LoadFactions()
{
    new 
query[400];
    for(new 
idid MAX_FACTIONSid++) // Goes through all the slots, looking for the data
    
{
        
format(querysizeof(query), "SELECT * FROM Factions WHERE ID = %d"id); // Selects all the information from the table
        
mysql_query(query);
        
mysql_store_result();
        if(
mysql_num_rows())
        if(
mysql_fetch_row_format(query,"|"))
        {
            
sscanf(query"p<|>e<is[64]ds[32]s[32]s[32]s[32]s[32]>",Factions[id]); // SSCANF seperates the data into the variables
            
Total_Factions_Created++; // Counts the factions created
        
}
    }
    
printf("> %d factions have been loaded from the database."Total_Factions_Created); // Prints out the information of how many factions created
    
return 1;

I'm trying to convert to threaded queries, but I'm doing it wrong as it doesn't work at all. If someone could help me convert it. That'd be great!

My table looks like this.



Re: converting unthreaded to threaded. - Gammix - 31.07.2016

1. You are performing select query before connection!
2. You can simply do all in one query by just giving a limit of MAX_FACTIONS and load all rows upto it.

pawn Код:
public OnGameModeInit()
{
    SetGameModeText("MySQL - Server");


    mysql_log(LOG_ALL);
    mysql = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);
    if(mysql_errno() != 0)
    {
        printf("[MySQL] The connection has failed.");
    }
    else
    {
        printf("[MySQL] The connection was successful.");
    }

    mysql_tquery(mysql, "SELECT * FROM `Factions` LIMIT "#MAX_FACTIONS"", "LoadFactions", "");
    return true;
}

forward LoadFactions();
public LoadFactions()
{
    new count = cache_get_row_count();
    for (new i; i < count; i++)
    {
        Factions[i][Type] = cache_get_field_content_int(i, "Type");
        cache_get_field_content(i, "Name", Factions[i][Name]);
        cache_get_field_content(i, "Rank1", Factions[i][Rank1]);
        cache_get_field_content(i, "Rank2", Factions[i][Rank2]);
        cache_get_field_content(i, "Rank3", Factions[i][Rank3]);
        cache_get_field_content(i, "Rank4", Factions[i][Rank4]);
         cache_get_field_content(i, "Rank5", Factions[i][Rank5]);
    }

    printf("> %d factions have been loaded from the database.", count); // Prints out the information of how many factions created
    return 1;
}
// I am not sure if you want "Factions[i][Type]" or is even in existence