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

[Tut] MySql - Save/Load/Usefull Things (Xp Mod)


Post New Thread Reply   
 
Thread Tools Display Modes
Jacob
Senior Member
Join Date: Oct 2010
Old 11-03-2010 , 02:40   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #41

Quote:
Originally Posted by grimvh2 View Post
PHP Code:
 format(szTemp,charsmax(szTemp),"UPDATE `tutorial` SET `exp` = '%i', `otherthing` = '%i', `astring` = '%s' WHERE `tutorial`.`steamid` = '%s';",iExp[id], otherthing[id], string[idszSteamId
Get it?
can you add a top15 for the Tut.
Jacob is offline
benjibau
Veteran Member
Join Date: Jul 2009
Location: France (Nord 59)
Old 11-03-2010 , 07:23   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #42

Quote:
Originally Posted by Jacob View Post
can you add a top15 for the Tut.
Nice idea
benjibau is offline
Send a message via MSN to benjibau
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-03-2010 , 13:35   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #43

Quote:
Originally Posted by Jacob View Post
can you add a top15 for the Tut.
It's only a simple query. The rest is things you should know from the tutorial (or another SQL tutorial) which is reading the 15 top players.

SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;

SELECT steamid, exp - Gets the steamid and exp from the table for each player
FROM tutorial - Gets data from tutorial table
ORDER BY exp DESC - Orders by the row's "exp" value descending so that the highest exp is first
LIMIT 15 - Limit 15 players in the results so if there are more than 15 they aren't used.

After executing this query, you would just need a simple loop:

Code:
new szSteamID[ 35 ], iExp; new iPos = 1; while( SQL_MoreResults( hQuery ) ) {     // SELECT steamid, exp     // steamid is first, so its column number is 0     // then exp, so its column number is 1     SQL_ReadResult( hQuery, 0, szSteamID, charsmax( szSteamID ) );     iExp = SQL_ReadResult( hQuery, 1 );         // print the player's top15 position and data     server_print( "#%i: %s - %i", iPos, szSteamID, iExp );         // increase to next top15 position and go to next row     iPos++;     SQL_NextRow( hQuery ); }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
benjibau
Veteran Member
Join Date: Jul 2009
Location: France (Nord 59)
Old 11-03-2010 , 14:37   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #44

Exolent i think if more good with board. I think is that ?

PHP Code:
public ShowTop15(id
{
    new 
szSteamID35 ], iExp;
    new 
iPos 1;
    new 
MenuBody[512], len;
    
len format(MenuBody511"\yStats TOP 15^n";)
    
    
Place++
    
SQL_ReadResult(Query,1,Name,32)
    
Points SQL_ReadResult(Query,2)
    
    
format(szTemp,charsmax(szTemp),"SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;")
    while( 
SQL_MoreResultshQuery ) )
    {
        
// SELECT steamid, exp
        // steamid is first, so its column number is 0
        // then exp, so its column number is 1
        
SQL_ReadResulthQuery0szSteamIDcharsmaxszSteamID ) );
        
iExp SQL_ReadResult(hQuery1);
    
          
// print the player's top15 position and data
        
len += format(MenuBody[len], 511-len"^n\r%d. %s %d Points"iPosszSteamIDiExp)
        
// increase to next top15 position and go to next row
        
iPos++;
        
SQL_NextRowhQuery );
    }

can you correct if is wrong please ?

Last edited by benjibau; 11-03-2010 at 14:40.
benjibau is offline
Send a message via MSN to benjibau
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-03-2010 , 15:17   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #45

Try testing before you post. That code won't even compile.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
benjibau
Veteran Member
Join Date: Jul 2009
Location: France (Nord 59)
Old 11-04-2010 , 15:01   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #46

Oh i think using SQL_ThreadQuery() in a func called with register_clcmd
and in the func i added this for choose steamid, exp from tutorial and after
PHP Code:
new Temp[512]
format(Temp,charsmax(Temp),"SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;")
SQL_ThreadQuery(g_SqlTuple,"myfunc",Temp,Data,1
for exemple:
PHP Code:
public ShowTop15(id)
{
    
//i must save stat for will haven't error into top15.
    //
    
new Data[1]
    
Data[0] = id
    
    
new Temp[512]
    
format(Temp,charsmax(Temp),"SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;")
    
SQL_ThreadQuery(g_SqlTuple,"Sql_Top15",Temp,Data,1)
    return 
PLUGIN_CONTINUE;
}

public 
Sql_Top15(FailState,Handle:hQuery,Error[],Errcode,Data[],DataSize
{
    new 
szSteamID35 ], iExp;
    new 
iPos 1;
    
    while( 
SQL_MoreResultshQuery ) )
    {
        
SQL_ReadResulthQuery0szSteamIDcharsmaxszSteamID ) );
        
iExp SQL_ReadResult(hQuery1); 
        
server_print"#%i: %s - %i"iPosszSteamIDiExp );
        
iPos++;
        
SQL_NextRowhQuery );
    }

edit: i tested with this but not work.
benjibau is offline
Send a message via MSN to benjibau
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-04-2010 , 18:45   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #47

PHP Code:
public ShowTop15(id)
{
    
//i must save stat for will haven't error into top15.
    //
    
new Data[1]
    
Data[0] = id
    
    
// no need for a variable since it's not being formatted
    /*new Temp[512]
    format(Temp,charsmax(Temp),"SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;")
    SQL_ThreadQuery(g_SqlTuple,"Sql_Top15",Temp,Data,1)*/
    
SQL_ThreadQuery(g_SqlTuple,"Sql_Top15","SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;",Data,1)
    return 
PLUGIN_CONTINUE;
}

public 
Sql_Top15(FailState,Handle:hQuery,Error[],Errcode,Data[],DataSize
{
    
// get player who is looking at top
    
new id Data];
    
    
// check for errors
    
if( FailState == TQUERY_CONNECT_FAILED
    
||  FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"Error on top15 query (%i): %s"ErrcodeError );
        
console_printid"Error on top15 query (%i): %s"ErrcodeError );
        return;
    }
    
    new 
szSteamID35 ], iExp;
    new 
iPos 1;
    
    while( 
SQL_MoreResultshQuery ) )
    {
        
SQL_ReadResulthQuery0szSteamIDcharsmaxszSteamID ) );
        
iExp SQL_ReadResulthQuery);
        
        
// print to player, not server 
        //server_print( "#%i: %s - %i", iPos, szSteamID, iExp );
        
console_printid"#%i: %s - %i"iPosszSteamIDiExp );
        
        
iPos++;
        
SQL_NextRowhQuery );
    }

__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Jacob
Senior Member
Join Date: Oct 2010
Old 11-04-2010 , 23:46   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #48

Quote:
Originally Posted by Exolent[jNr] View Post
PHP Code:
public ShowTop15(id)
{
    
//i must save stat for will haven't error into top15.
    //
    
new Data[1]
    
Data[0] = id
 
    
// no need for a variable since it's not being formatted
    /*new Temp[512]
    format(Temp,charsmax(Temp),"SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;")
    SQL_ThreadQuery(g_SqlTuple,"Sql_Top15",Temp,Data,1)*/
    
SQL_ThreadQuery(g_SqlTuple,"Sql_Top15","SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;",Data,1)
    return 
PLUGIN_CONTINUE;
}
 
public 
Sql_Top15(FailState,Handle:hQuery,Error[],Errcode,Data[],DataSize
{
    
// get player who is looking at top
    
new id Data];
 
    
// check for errors
    
if( FailState == TQUERY_CONNECT_FAILED
    
||  FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"Error on top15 query (%i): %s"ErrcodeError );
        
console_printid"Error on top15 query (%i): %s"ErrcodeError );
        return;
    }
 
    new 
szSteamID35 ], iExp;
    new 
iPos 1;
 
    while( 
SQL_MoreResultshQuery ) )
    {
        
SQL_ReadResulthQuery0szSteamIDcharsmaxszSteamID ) );
        
iExp SQL_ReadResulthQuery);
 
        
// print to player, not server 
        //server_print( "#%i: %s - %i", iPos, szSteamID, iExp );
        
console_printid"#%i: %s - %i"iPosszSteamIDiExp );
 
        
iPos++;
        
SQL_NextRowhQuery );
    }

Thanks you,Exolent !
Jacob is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-05-2010 , 16:35   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #49

@grimvh

Your tutorial has errors that is spreading to users who copy/paste this for their own SQL saving.

Quote:
Originally Posted by grimvh2 View Post
Now our final code will look like this :

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

#define PLUGIN "Tutorial"
#define VERSION "1.0"
#define AUTHOR "Grim"

// Ur Mysql Information
new Host[]     = "hostname"
new User[]    = "username"
new Pass[]     = "password"
new Db[]     = "database"


new Handle:g_SqlTuple
new g_Error[512]


new 
iExp[33]

// Pcvar's
new cKill
new cHeadshot
new cDeath

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_event("DeathMsg""Event_DeathMsg""a"// Register death event
    
    // register the Pcvar's
    
cKill register_cvar("exp_kill""2")
    
cHeadshot register_cvar("exp_headshot""4")
    
cDeath register_cvar("exp_death""1")
    
    
set_task(1.0"MySql_Init"// set a task to activate the mysql_init
}

public 
MySql_Init()
{
    
// we tell the API that this is the information we want to connect to,
    // just not yet. basically it's like storing it in global variables
    
g_SqlTuple SQL_MakeDbTuple(Host,User,Pass,Db)
   
    
// ok, we're ready to connect
    
new ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    if(
SqlConnection == Empty_Handle)
        
// stop the plugin with an error message
        
set_fail_state(g_Error)
       
    new 
Handle:Queries
    
// we must now prepare some random queries
    
Queries SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS tutorial (steamid varchar(32),exp INT(11))")

    if(!
SQL_Execute(Queries))
    {
        
// if there were any problems
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
        
set_fail_state(g_Error)
       
    }
    
    
// close the handle
    
SQL_FreeHandle(Queries)
   
    
// you free everything with SQL_FreeHandle
    
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)
}

public 
Load_MySql(id)
{
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    
    if(
g_SqlTuple == Empty_Handle)
        
set_fail_state(g_Error)
        
    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 `tutorial` WHERE (`tutorial`.`steamid` = '%s')"szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
    
    
SQL_FreeHandle(SqlConnection)
}

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]
        
        
// now we will insturt the values into our table.
        
format(szTemp,charsmax(szTemp),"INSERT INTO `tutorial` ( `steamid` , `exp`)VALUES ('%s','0');",szSteamId)
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    } 
    else 
    {
        
// if there are results found
        
iExp[id]         = SQL_ReadResult(Query1)
    }
    
    return 
PLUGIN_HANDLED
}

public 
Save_MySql(id)
{
    new 
szSteamId[32], szName[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 `tutorial` SET `exp` = '%i' WHERE `tutorial`.`steamid` = '%s';",iExp[id], szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}

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

public 
client_putinserver(id)
{
    
Load_MySql(id)
}

public 
client_disconnect(id)
{
    
Save_MySql(id)
}

public 
Event_DeathMsg()
{
    new 
iKiller read_data(1// read the data to get the killer and victim
    
new iVictim read_data(2)
    
    if(
is_user_alive(iKiller)) // Check if the killer is alive in case he killed himself
    
{
        if(
read_data(3))
        {
            
iExp[iKiller] += get_pcvar_num(cHeadshot// Add the amount of the Pcvar to iExp
        
}
        else
        {
            
iExp[iKiller] += get_pcvar_num(cKill)
        }
    }
    
iExp[iVictim] -= get_pcvar_num(cDeath// Decrease the amount of the Pcvar from iExp

The problem is here:
PHP Code:
public Load_MySql(id)
{
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    
    if(
g_SqlTuple == Empty_Handle)
        
set_fail_state(g_Error)
        
    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 `tutorial` WHERE (`tutorial`.`steamid` = '%s')"szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
    
    
SQL_FreeHandle(SqlConnection)

SQL_Connect() is for direct queries using SQL_PrepareQuery() and SQL_ThreadQuery() does not require a connection because it is set to a thread and connect/execute when it can.

It should be:
PHP Code:
public Load_MySql(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 `tutorial` WHERE (`tutorial`.`steamid` = '%s')"szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)

__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
FEELiFE
Member
Join Date: Jul 2010
Old 11-07-2010 , 10:51   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #50

Quote:
Originally Posted by Exolent[jNr] View Post
It's only a simple query. The rest is things you should know from the tutorial (or another SQL tutorial) which is reading the 15 top players.

SELECT steamid, exp FROM tutorial ORDER BY exp DESC LIMIT 15;

SELECT steamid, exp - Gets the steamid and exp from the table for each player
FROM tutorial - Gets data from tutorial table
ORDER BY exp DESC - Orders by the row's "exp" value descending so that the highest exp is first
LIMIT 15 - Limit 15 players in the results so if there are more than 15 they aren't used.

After executing this query, you would just need a simple loop:

Code:
new szSteamID[ 35 ], iExp;
new iPos = 1;

while( SQL_MoreResults( hQuery ) ) { // SELECT steamid, exp // steamid is first, so its column number is 0 // then exp, so its column number is 1 SQL_ReadResult( hQuery, 0, szSteamID, charsmax( szSteamID ) );
iExp = SQL_ReadResult( hQuery, 1 );

// print the player's top15 position and data server_print( "#%i: %s - %i", iPos, szSteamID, iExp );

// increase to next top15 position and go to next row iPos++; SQL_NextRow( hQuery );
}

Question about this: How can I select the exp where player name = current player name?
FEELiFE 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 12:46.


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