AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Saving Player steamid to SQL (https://forums.alliedmods.net/showthread.php?t=308676)

SANTO37 06-29-2018 16:47

[HELP] Saving Player steamid to SQL
 
Hi, I have made a website. I need to record the SteamId information of the players in the base. The steam information will be saved to the database when you write the number / steam.

If the database does not have steam information of the player;
PHP Code:

client_print(idprint_chat"We've already registered you on the system."

If the database also has steam information of the player;
PHP Code:

client_print(idprint_chat"Registration Successful."

I would like to thank the friends who can help me.

In the following code, the information is not saved immediately and the information of other players is added.
This code is not working properly;
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <sqlx>

// Ur Mysql Information
new Host[]    =    "127.0.0.1"
new User[]    =    "amx_mac"
new Pass[]    =    "amx_mac"
new Db[]    =    "amx_mac"

new Handle:g_SqlTuple
new g_Error[512]
new 
iData[33]

public 
plugin_init() {
    
register_clcmd("say /v""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 (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, nameid varchar(32), steamid varchar(32), data 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 
szName[32], szSteamId[32], szTemp[512]
    
get_user_authid(id,szSteamId,31)
    
get_user_name(idszName,31)
    
    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`.`nameid` = '%s')"szName)
    
SQL_ThreadQuery(g_SqlTuple"register_client"szTempData1)
}

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 szName[32], szSteamId[32]
        
get_user_authid(idszSteamId,31)    // get user's steamid
        
get_user_name(idszName,31)            // get user's nameid
        
        //  if its still pending we can't do anything with it
        
if (equal(szName"ID_PENDING")) return PLUGIN_HANDLED
        
        
new szTemp[512]
        
// now we will insturt the values into our table.
        
format(szTemp,charsmax(szTemp), "INSERT INTO `tutorial` ( `nameid` , `steamid` , `data`) VALUES ('%s' , '%s' , '0');"szNameszSteamId)
        
SQL_ThreadQuery(g_SqlTuple"IgnoreHandle"szTemp)
    } else {
        
// if there are results found
        
iData[id] = SQL_ReadResult(Query0)
    }
    return 
PLUGIN_HANDLED
}

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

public 
client_putinserver(idLoad_MySql(id


Kushfield 06-30-2018 04:53

Re: [HELP] Saving Player steamid to SQL
 
I didn't really understand what the problem is, but maybe this is the cause?
PHP Code:

    //we will now select from the table `tutorial` where the steamid match
    
format(szTemp,charsmax(szTemp), "SELECT * FROM `tutorial` WHERE (`tutorial`.`nameid` = '%s')"szName)
    
SQL_ThreadQuery(g_SqlTuple"register_client"szTempData1

According to both the comment and just logical thinking, you want to check for the SteamID here, but you're actually checking for the name.

If that's not it, could you please explain what you mean by "In the following code, the information is not saved immediately and the information of other players is added." or what the exact issue is with the code.

Natsheh 06-30-2018 05:18

Re: [HELP] Saving Player steamid to SQL
 
Simply because you have closed the connection , create the tuple and the connection in plugin init.

And dont free them until plugin ends

SANTO37 06-30-2018 05:34

Re: [HELP] Saving Player steamid to SQL
 
I created a website where players can subscribe.
They have to enter the game [say / steam] and register their steamid address.
However, the plugin registers all the players' data at the same time.
I just want [say / steam] to be registered.
The player registers after logging out from the server. What do I have to do to register?

Kushfield 06-30-2018 05:37

Re: [HELP] Saving Player steamid to SQL
 
Quote:

Originally Posted by Natsheh (Post 2600014)
Simply because you have closed the connection , create the tuple and the connection in plugin init.

And dont free them until plugin ends

Apart from creating the table, he's only using threaded queries, which don't need an already active connection.

SANTO37 06-30-2018 05:42

Re: [HELP] Saving Player steamid to SQL
 
Plugin... Could you write it all over again?

Kushfield 06-30-2018 05:58

Re: [HELP] Saving Player steamid to SQL
 
Quote:

Originally Posted by SANTO37 (Post 2600019)
I created a website where players can subscribe.
They have to enter the game [say / steam] and register their steamid address.
However, the plugin registers all the players' data at the same time.
I just want [say / steam] to be registered.
The player registers after logging out from the server. What do I have to do to register?

Ok, so right now your plugin works like this:
1. Do nothing except generate errors until someone joins and writes /v (MySql_Init)
2. When a player joins, check if his/her name exists in the database table
3. If it doesn't, insert a new record with the player's information

Assuming you want it to:
1. When a player writes /steam, check if his/her SteamID exists in the database table
2. If it doesn't, insert a new record with the player's information

You should:
1. Add "MySql_Init()" directly in plugin_init() instead of registering it as a command, so that the database connection information is always available via g_SqlTuple
2. Register the "say /steam" client command instead, setting Load_MySql as it's handling function
3. Remove "Load_MySql(id)" from client_putinserver

SANTO37 06-30-2018 07:04

Re: [HELP] Saving Player steamid to SQL
 
Excellent !
But I want the [say / steam] command to work.
You could ask me if you could.
I'm new.

SANTO37 06-30-2018 07:11

Re: [HELP] Saving Player steamid to SQL
 
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <sqlx>

// Ur Mysql Information
new Host[]    =    "127.0.0.1"
new User[]    =    "amx_mac"
new Pass[]    =    "amx_mac"
new Db[]    =    "amx_mac"

new Handle:g_SqlTuple
new g_Error[512]
new 
iData[33]

public 
plugin_init() {
    
register_clcmd("say /steam""Load_MySql")
    
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 (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, nameid varchar(32), steamid varchar(32), data 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 
szName[32], szSteamId[32], szTemp[512]
    
get_user_authid(id,szSteamId,31)
    
get_user_name(idszName,31)
    
    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`.`nameid` = '%s')"szName)
    
SQL_ThreadQuery(g_SqlTuple"register_client"szTempData1)
}

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 szName[32], szSteamId[32]
        
get_user_authid(idszSteamId,31)    // get user's steamid
        
get_user_name(idszName,31)            // get user's nameid
        
        //  if its still pending we can't do anything with it
        
if (equal(szName"ID_PENDING")) return PLUGIN_HANDLED
        
        
new szTemp[512]
        
// now we will insturt the values into our table.
        
format(szTemp,charsmax(szTemp), "INSERT INTO `tutorial` ( `nameid` , `steamid` , `data`) VALUES ('%s' , '%s' , '0');"szNameszSteamId)
        
SQL_ThreadQuery(g_SqlTuple"IgnoreHandle"szTemp)
    } else {
        
// if there are results found
        
iData[id] = SQL_ReadResult(Query0)
    }
    return 
PLUGIN_HANDLED
}

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


thank you so much.

Kushfield 06-30-2018 07:11

Re: [HELP] Saving Player steamid to SQL
 
Quote:

Originally Posted by SANTO37 (Post 2600058)
Excellent !
But I want the [say / steam] command to work.
You could ask me if you could.
I'm new.

If you're asking how to do point 2, then just add this in plugin_init:
PHP Code:

register_clcmd("say /steam""Load_MySql"

EDIT:
I see you did that already. Not sure what it is you're asking for then.
By the way, you're still checking for the existence of Name, not SteamID in the table. Maybe that's the intention, but according to your description, it doesn't seem like it.


All times are GMT -4. The time now is 12:48.

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