AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Wrong data loading from SQL (https://forums.alliedmods.net/showthread.php?t=132443)

Backstabnoob 07-15-2010 06:56

Wrong data loading from SQL
 
Hi there. What's wrong with the code below? I don't know why, but somehow "Aid" is anywhere 0, even though in the table it is for example 5. The admin nickname getting should work, as long as it's showing fine in the log. It's equal to "nickname" in the table. Thank's in advance.


PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <sqlx>
#include <amxbans_core>


new Handleg_SqlTuple
new g_Cache[1024]

public 
plugin_init()
{
    
register_plugin("Admin Online Time""0.1""IdiotStrike")
    
    new 
Host[64],User[64],Pass[64],Db[64]
    
    
get_cvar_string("amx_sql_host",Host,63)
    
get_cvar_string("amx_sql_user",User,63)
    
get_cvar_string("amx_sql_pass",Pass,63)
    
get_cvar_string("amx_sql_db",Db,63)
    
    
g_SqlTuple SQL_MakeDbTuple(Host,User,Pass,Db)
}

public 
amxbans_admin_disconnect(id)
{
    new 
nick[36]
    
amxbans_get_admin_nick(idnick35)
    
    new 
Time get_user_time(id1)    
    
    new 
iData[1]

    
iData[0] = Time
    
    
    formatex
(g_Cache1023"SELECT * from amxnew_amxadmins where nickname LIKE '%%s%'"nick)
    
SQL_ThreadQuery(g_SqlTuple"Select"g_CacheiData2)
    
    
log_to_file("testing.log""Admin odpojen: %s; Time = %d"nickTime)
    
}

public 
Select(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
        return 
set_fail_state("Could not connect to SQL database.")
    else if(
FailState == TQUERY_QUERY_FAILED)
        return 
log_amx("Query failed")
   
    if(
Errcode)
        return 
log_amx("Query failed")
        
    new 
Time Data[0]
    new 
Aid
    
    
while(SQL_MoreResults(Query))
        
Aid SQL_ReadResult(Query0)
    
    
log_to_file("testing.log""Nactena data: %d; Time = %d"AidTime)
    
    
    new 
TheData[2]
    
TheData[0] = Aid
    TheData
[1] = Time
    
    SaveAdmin
(TheData)

    return 
PLUGIN_CONTINUE
}
    

public 
SaveAdmin(iData[2])
{
    
    new 
Aid iData[0]
    new 
Time iData[1]
    
    
formatex(g_Cache1023"INSERT INTO amxnew_onlinetime VALUES ('%d', '%d', '%d') ON DUPLICATE KEY UPDATE admin_time=admin_time+%d"Aid ,Timeget_systime(), Time
    
SQL_ThreadQuery(g_SqlTuple"Handler"g_Cache)
    
    
log_to_file("testing.log""Snazim se ulozit informace admina: %d; Time = %d"AidTime)
    
}

public 
Handler(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
        return 
set_fail_state("Could not connect to SQL database.")
    else if(
FailState == TQUERY_QUERY_FAILED)
        return 
log_amx("Query failed")
   
    if(
Errcode)
        return 
log_amx("Query failed")
        
    
log_to_file("testing.log""Uspesne ulozeno.")
    
    return 
PLUGIN_CONTINUE


Edit: oh and yeah, in amxnew_amxadmins id is the first column, so Aid = SQL_ReadResult(Query, 0) should load it just fine. I think there is something wrong with that " where nickname LIKE "%%s%" ", but I'm not sure.

Edit2: where nickname='%s' doesn't work either - still returns 0 in Aid

Sylwester 07-15-2010 16:12

Re: Wrong data loading from SQL
 
1. You should have posted table structure...
2. Use "... where nickname='%s';"
3. Try this (don't forget to replace column_name with name of id column in your table):
PHP Code:

    if(SQL_MoreResults(Query)){
        
Aid SQL_ReadResult(QuerySQL_FieldNameToNum(Query,"column_name"))  //replace column_name with id column name !
        
log_to_file("testing.log""Nactena data: %d; Time = %d"AidTime)
    }else{
        
log_to_file("testing.log""No data found")
    } 

4. If you used "select * from ..." then print all retrieved data, it makes things easier when debugging.
PHP Code:

    if(SQL_MoreResults(Query)){
        
//...
        
iValue1 SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"iColumn1"))
        new 
szValue2[32]
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"szColumn2"), szValue231)
        
//...
        
log_to_file("testing.log""iValue1: %d, szValue2: %s ...."iValue1szValue2, ...)
    }else{
        
log_to_file("testing.log""No data found")
    } 



All times are GMT -4. The time now is 07:09.

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