Raised This Month: $ Target: $400
 0% 

Need mysql base


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
reinert
Veteran Member
Join Date: Feb 2007
Old 06-08-2010 , 10:26   Re: Need mysql base
Reply With Quote #1

Why this code is not working ? where is problem:

PHP Code:
CHECK NEW CODE 
code:
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>

#define SQL_HOST "localhost"
#define SQL_USER "root"
#define SQL_PASS "mypw"
#define SQL_DB "users"


new Handle:g_SqlTuple
new g_Error[512]

public 
plugin_init()
{
    
register_plugin("SQLx test","1.0","reinert")
   
    
g_SqlTuple SQL_MakeDbTuple(SQL_HOST,SQL_USER,SQL_PASS,SQL_DB)
   
}

public 
client_connect(id)
{
    new 
szName[33]
    
get_user_name(idszName32)
    
    new 
szIp[33]
    
get_user_ip(idszIp32)
    
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)    
    
    new 
Handle:Query SQL_PrepareQuery(SqlConnection"SELECT * FROM `test` WHERE `ip`='%s'"szIp)
    
    if(!
SQL_Execute(Query))
    {
        
// if there were any problems
    
SQL_QueryError(Query,g_Error,511)
    
log_amx(g_Error)
    }

    
    if(!
SQL_NumResults(Query)) {
        new 
va[33]
        
formatex(va32"INSERT INTO test (name, ip) VALUES ('%s', '%s')"szNameszIp);
                
SQL_PrepareQuery(SqlConnectionva)
    }
    
    if(
g_SqlTuple == Empty_Handle)
         
log_amx(g_Error)

}
public 
client_disconnect(id)
{   
    
    new 
szName[33]
    
get_user_name(idszName32)
    
    new 
szIp[33]
    
get_user_ip(idszIp32)

    
// ok, we're ready to connect
    
new ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
    if(
g_SqlTuple == Empty_Handle)
         
log_amx(g_Error)
   
    
// run a random query
    
new Handle:Query SQL_PrepareQuery(SqlConnection,"UPDATE `test` SET `name`='%s' WHERE `ip`='%s'",szNameszIp)  
   
    
// run the query
    
if(!SQL_Execute(Query))
    {
        
// if there were any problems
         
SQL_QueryError(Query,g_Error,511)
         
log_amx(g_Error)
    }
   
    
// checks to make sure there's more results
    // notice that it starts at the first row, rather than null
    
new Data
    
while(SQL_MoreResults(Query))
    {
        
// columns start at 0
        
Data SQL_ReadResult(Query,0)
       
        
server_print("Found data: %d",Data)

        
SQL_NextRow(Query)
    }
   
    
// of course, free the handle
    
SQL_FreeHandle(Query)
   
    
// and of course, free the connection
    
SQL_FreeHandle(SqlConnection)
}

public 
plugin_end()
    
// free the tuple - note that this does not close the connection,
    // since it wasn't connected in the first place
    
SQL_FreeHandle(g_SqlTuple
Checked my test table, its empty. why... ? Nothing in logs.

Last edited by reinert; 06-09-2010 at 16:41.
reinert is offline
Old 06-09-2010, 16:41
reinert
This message has been deleted by YamiKaitou. Reason: no bump
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 06-09-2010 , 17:41   Re: Need mysql base
Reply With Quote #3

Using prepare and execute query is not a good way to use sqlx.

This is how you do it:
PHP Code:
#include <amxmodx>
#include <sqlx>

new Handle:g_SqlTuple
new const g_sqlTable[] = "test_table"


public plugin_init(){
    
register_plugin("sqlite test""1.0""Sylwester")
    
sql_init()
    
register_clcmd("table_create""check_table")
    
register_clcmd("table_save""table_save")
    
register_clcmd("table_load""table_load")
    
register_clcmd("table_loadx""table_loadx")
}


public 
sql_init(){
    
SQL_SetAffinity("sqlite")
    
g_SqlTuple SQL_MakeDbTuple("127.0.0.1","root","","test_db")
}


public 
check_table(){
    new 
cache[512]
    
formatex(cache511"CREATE TABLE IF NOT EXISTS %s ( name VARCHAR( 64 ), ip VARCHAR( 64 ),authid VARCHAR( 64 ), class integer( 2 ) , lvl integer( 3 ) DEFAULT 1, exp integer( 9 ) DEFAULT 0, str integer( 3 ) DEFAULT 0, int integer( 3 ) DEFAULT 0, dex integer( 3 ) DEFAULT 0, agi integer( 3 ) DEFAULT 0, PRIMARY KEY ( `authid`,`class`) );",g_sqlTable)
    
log_amx("create table query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_create_table"cache)
    return 
PLUGIN_HANDLED
}


public 
handle_create_table(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        
log_amx("table %s creation failed"g_sqlTable)
        return 
PLUGIN_HANDLED
    
}

    
log_amx("table %s created..."g_sqlTable)
    return 
PLUGIN_HANDLED
}


public 
table_save(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)
    new class = 
str_to_num(args)

    new 
cache[512]
    new 
steamid[32], ip[32], name[32]
    
get_user_name(idname31)
    
get_user_authid(idsteamid31)
    
get_user_ip(idip31)
    
formatex(cache511,"REPLACE INTO `%s` ( `authid` , `class` ,`name` , `ip` , `lvl` , `exp`, `str` , `int` , `dex` , `agi` ) VALUES ( '%s', '%d', '%s', '%s', '%d', '1337', '%d', '2', '3', '%d' );"g_sqlTablesteamid, class, nameip, class*10+1, class, random(100))
    
log_amx("save query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_save"cache)
    return 
PLUGIN_HANDLED
}


public 
handle_save(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    return 
PLUGIN_HANDLED
}


public 
table_load(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)
    new class = 
str_to_num(args)    

    new 
cache[512]
    new 
steamid[32]
    
get_user_authid(idsteamid31)
    
formatex(cache511,"SELECT * FROM %s WHERE ( authid='%s' AND class='%d' );"g_sqlTablesteamid, class)
    
log_amx("load query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_load"cache)
    return 
PLUGIN_HANDLED

}


public 
handle_load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    if(
SQL_MoreResults(Query)){
        new 
name[32], steamid[32], ip[32]
        new 
lvl SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"lvl"))
        new 
xp SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"exp"))
        
        new 
int SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"int"))
        new 
str SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"str"))
        new 
agi SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"agi"))
        new 
dex SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"dex"))
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"name"), name31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"ip"), ip31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"authid"), steamid31)
        
log_amx("retrieved result for %s %s %s, lvl %d xp %d, int %d, str %d, agi %d, dex %d"namesteamidiplvlxpintstragidex)

    }else{
        
log_amx("retrieved no results ....")
    }

    return 
PLUGIN_HANDLED
}


public 
table_loadx(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)

    new 
cache[512]
    new 
steamid[32]
    
get_user_authid(idsteamid31)
    
formatex(cache511,"SELECT * FROM %s WHERE authid='%s';"g_sqlTablesteamid)
    
log_amx("load query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_loadx"cache)
    return 
PLUGIN_HANDLED

}


public 
handle_loadx(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    if(!
SQL_MoreResults(Query)){
        
log_amx("retrieved no results ....")
        return 
PLUGIN_HANDLED
    
}

    do{
        new 
name[32], steamid[32], ip[32]
        new class = 
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"class"))
        new 
lvl SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"lvl"))
        new 
xp SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"exp"))
        
        new 
int SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"int"))
        new 
str SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"str"))
        new 
agi SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"agi"))
        new 
dex SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"dex"))
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"name"), name31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"ip"), ip31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"authid"), steamid31)
        
log_amx("got res for %s %s %s, class %d, lvl %d, xp %d, int %d, str %d, agi %d, dex %d"namesteamidip, class, lvlxpintstragidex)
        
SQL_NextRow(Query)
    }while(
SQL_MoreResults(Query))

    return 
PLUGIN_HANDLED

You don't need mysql server to test this. You only need sqlite in your modules.ini
table_load/save has 1 argument - number of class
__________________
Impossible is Nothing
Sylwester is offline
ARES[ro]
Senior Member
Join Date: Apr 2010
Old 01-06-2011 , 14:54   Re: Need mysql base
Reply With Quote #4

got it .
__________________
okay

Last edited by ARES[ro]; 01-06-2011 at 15:31. Reason: nvm
ARES[ro] 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 05:16.


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