Raised This Month: $51 Target: $400
 12% 

MySql save/load problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
spirit
Senior Member
Join Date: Jul 2009
Location: Quimper
Old 05-05-2012 , 12:35   MySql save/load problem
Reply With Quote #1

Hi all,


I using trying to made save and load with sqlx
But it's not saving and not loading

Code:
#include <amxmodx>
#include <sqlx>

new g_jobclass[33], money[33], MoneyAccount[33]

new Handle:g_SqlTuple

new g_Error[512]
new g_UserClass[33], tune[33], bank[33]

new Host[]     = "mysqladdress"
new User[]    = "dbname"
new Pass[]     = "mydbpass"
new Db[]     = "dbname"

public plugin_init()
{
    set_task(1.0, "MySql_Init")
    register_clcmd("say /save", "Save_MySql")
    register_clcmd("say /load", "Load_MySql")
}

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 myfirsttable (steamid varchar(64) NOT NULL, name varchar(64) NOT NULL, jobid INT(11), money INT(11), bank 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 client_authorized(id)
{
    Load_MySql(id)
}

public client_disconnect(id)
{
    Save_MySql(id)
}

public Load_MySql(id)
{
    new SteamId[32], szTemp[512]
    get_user_authid(id, SteamId, charsmax(SteamId))
    
    new Data[1]
    Data[0] = id
    
    format(szTemp,charsmax(szTemp),"SELECT * FROM `myfirsttable` WHERE (`myfirsttable`.`steamid` = '%s')", SteamId)
    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", Errcode, Error)
    }
    else if(FailState == TQUERY_QUERY_FAILED)
    {
        log_amx("Load Query failed. [%d] %s", Errcode, Error)
    }
    
    new id
    id = Data[0]
    
    if(SQL_NumResults(Query) < 1) 
    {
        new SteamId[32], name[32]
        get_user_authid(id, SteamId, charsmax(SteamId)) 
        get_user_authid(id, name, charsmax(name)) 
        
        if (equal(SteamId,"ID_PENDING"))
            return PLUGIN_HANDLED;
        
        new szTemp[512]
        
        format(szTemp,charsmax(szTemp),"INSERT INTO `myfirsttable` (`steamid`, `name`, `jobid`, `argent`, `bank`) VALUES ('%s','%s', '0', '5000', '0');", SteamId, name)
        SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    } 
    else 
    {
        g_UserClass[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query,"jobid"))
        tune[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query,"argent"))
        bank[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query,"bank"))
    }
    if(is_user_connected(id))
    {
        client_print(id, print_chat, "userclass %d et money %d, bank: %d", g_UserClass[id], tune[id], bank[id])
        money[id] = tune[id]
        g_jobclass[id] = g_UserClass[id]
        MoneyAccount[id] = money[id]
    }
    return PLUGIN_HANDLED;
}

public Save_MySql(id)
{
    new SteamId[32], name[32], szTemp[512]
    get_user_name(id, name, charsmax(name))
    get_user_authid(id, SteamId, charsmax(SteamId))
        
    format(szTemp,charsmax(szTemp),"UPDATE `myfirsttable` SET `jobid` = '%d', `money` = '%d', `bank` = '%d', `name` = '%s'  WHERE `myfirsttable`.`steamid` = '%s';", g_jobclass[id], money[id], MoneyAccount[id], name, SteamId)
    SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    client_print(id, print_chat, "jobid: %d money %d", g_jobclass[id], money[id])
}

public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    SQL_FreeHandle(Query)
    return PLUGIN_HANDLED;
}
spirit is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 05-05-2012 , 12:44   Re: MySql save/load problem
Reply With Quote #2

http://www.w3schools.com/sql/sql_update.asp
__________________
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
spirit
Senior Member
Join Date: Jul 2009
Location: Quimper
Old 05-05-2012 , 13:18   Re: MySql save/load problem
Reply With Quote #3

hmm this parts is my error ?WHERE `myfirsttable`.`steamid` = '%s'

Code:
public Save_MySql(id)
{
    new SteamId[32], name[32], szTemp[512]
    get_user_name(id, name, charsmax(name))
    get_user_authid(id, SteamId, charsmax(SteamId))
        
    format(szTemp,charsmax(szTemp),"UPDATE `myfirsttable` SET jobid = ^"%d^", money = ^"%d^", bank = ^"%d^", name = ^"%s^"  WHERE steamid = ^"%s^";", g_jobclass[id], money[id], MoneyAccount[id], name, SteamId)
    SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}

Last edited by spirit; 05-05-2012 at 13:24.
spirit is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:37.


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