AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Save & Load Query Problem. (https://forums.alliedmods.net/showthread.php?t=170810)

rainin 10-29-2011 06:13

Save & Load Query Problem.
 
Hello.

I have problem with a Load and Save Query.

PHP Code:

public MySql_Init()
{
    
g_SqlTuple SQL_MakeDbTuple(Host,User,Pass,Db);
    
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error));
    if(
SqlConnection == Empty_Handle)
    {
        
set_fail_state(g_Error);
    }
    new 
Handle:Queries;
    
Queries SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS dr_stats (nick varchar(255), auth varchar(255), elud int(11), money int(11))");
    
    if(!
SQL_Execute(Queries))
    {
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error));
        
set_fail_state(g_Error);
    }
    
    
SQL_FreeHandle(Queries);
    
SQL_FreeHandle(SqlConnection);
}

public 
LoadData(id)
{
    new 
szSteamId[32], szTemp[512];
    
get_user_authid(idszSteamIdcharsmax(szSteamId));
    
    new 
Data[1];
    
Data[0] = id;
    
    
//we will now select from the table `tutorial` where the steamid match
    
format(szTemp,charsmax(szTemp),"SELECT * FROM `dr_stats` WHERE `auth` = '%s'"szSteamId);
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1);
}

public 
register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
    {
        
log_amx("Load - Could not connect to SQL database.  [%d] %s"ErrcodeError);
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError);
    }

    new 
id;
    
id Data[0];
    
    if(
SQL_NumResults(Query) < 1
    {
        
//.if there are no results found
        
        
new szSteamId[32];
        
get_user_authid(idszSteamIdcharsmax(szSteamId)); // get user's steamid
        
        //  if its still pending we can't do anything with it
        
if (equal(szSteamId,"ID_PENDING"))
            return 
PLUGIN_HANDLED;
            
        new 
szTemp[512];
    
        new 
name[33];
        
get_user_name(idname32);
        
format(szTemp,charsmax(szTemp),"INSERT INTO `dr_stats` (`nick`, `auth`, `elud`, `money`) VALUES ('%s','%s','0','0');",nameszSteamId);
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp);
    } 
    else 
    {
        
// if there are results found
        
g_Lifes[id]         = SQL_ReadResult(Query1);
    }
    
    return 
PLUGIN_HANDLED;
}

public 
SaveData(id)
{
    new 
szSteamId[32], szTemp[512];
    
get_user_authid(idszSteamIdcharsmax(szSteamId));
    
    
// Here we will update the user hes information in the database where the steamid matches.
    
format(szTemp,charsmax(szTemp),"UPDATE `dr_stats` SET `elud` = '%s' WHERE `dr_stats`.`auth` = '%s';",g_Lifes[id], szSteamId);
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp);
}

public 
IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
SQL_FreeHandle(Query);
    
    return 
PLUGIN_HANDLED;


If i connect to the server, it doesnt get informatsion from sql database and it wont save data if i change the Map.

Backstabnoob 10-29-2011 07:59

Re: Save & Load Query Problem.
 
You don't need to connect to SQL via SQL_Connect(), just create a tuple in plugin_cfg and make the threaded query there (to create table).

Otherwise I cannot see anything wrong, check the logs to see possible SQL failures.

By the way; you don't have to free the query in IgnoreHandle as it's a threaded query.

Sylwester 10-29-2011 16:16

Re: Save & Load Query Problem.
 
Quote:

Originally Posted by rainin (Post 1585533)
If i connect to the server, it doesnt get informatsion from sql database and it wont save data if i change the Map.

It probably saves the data, but I'm not sure, because you didn't paste function that calls SaveData(id). Mapchange has nothing to do with it.

It doesn't load data because you made a mistake.
PHP Code:

g_Lifes[id] = SQL_ReadResult(Query1); 

should be:
PHP Code:

g_Lifes[id] = SQL_ReadResult(Query2); 

(elud is in 3rd colum, so you need to use 2 or SQL_FieldNameToNum(Query, "elud") to avoid this problem later if you change table definition)

IgnoreHandle should look like this:
PHP Code:

public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s"Error);
        return;
    }



rainin 10-29-2011 17:09

Re: Save & Load Query Problem.
 
Quote:

Originally Posted by Sylwester (Post 1585953)
It probably saves the data, but I'm not sure, because you didn't paste function that calls SaveData(id). Mapchange has nothing to do with it.

It doesn't load data because you made a mistake.
PHP Code:

g_Lifes[id] = SQL_ReadResult(Query1); 

should be:
PHP Code:

g_Lifes[id] = SQL_ReadResult(Query2); 

(elud is in 3rd colum, so you need to use 2 or SQL_FieldNameToNum(Query, "elud") to avoid this problem later if you change table definition)

IgnoreHandle should look like this:
PHP Code:

public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s"Error);
        return;
    }



PHP Code:

public client_putinserverid )
{
    
LoadData(id);
    
g_bConnectedid ] = true;
}
   
public 
client_disconnectid ) {
   
g_bConnectedid ] = false;
   
SaveData(id);
   
CheckTerrorists( );
   
   if( !
g_bRestart && is_valid_entg_iThinker ) )
      
entity_set_floatg_iThinkerEV_FL_nextthinkget_gametime( ) + 15.0 );
   
#if defined FAKE_PLAYER
   
if( g_iFakeplayer == id ) {
      
set_task1.5"UpdateBot" );
      
      
g_iFakeplayer 0;
   }
#endif




All times are GMT -4. The time now is 14:24.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.