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

[HELP] Saving Player steamid to SQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SANTO37
Member
Join Date: Aug 2008
Old 06-29-2018 , 16:47   [HELP] Saving Player steamid to SQL
Reply With Quote #1

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

Last edited by SANTO37; 06-29-2018 at 16:49.
SANTO37 is offline
Kushfield
Member
Join Date: Jan 2017
Location: Estonia
Old 06-30-2018 , 04:53   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #2

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.
Kushfield is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-30-2018 , 05:18   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #3

Simply because you have closed the connection , create the tuple and the connection in plugin init.

And dont free them until plugin ends
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 06-30-2018 at 05:19.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
SANTO37
Member
Join Date: Aug 2008
Old 06-30-2018 , 05:34   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #4

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?
SANTO37 is offline
Kushfield
Member
Join Date: Jan 2017
Location: Estonia
Old 06-30-2018 , 05:37   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #5

Quote:
Originally Posted by Natsheh View Post
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.
Kushfield is offline
SANTO37
Member
Join Date: Aug 2008
Old 06-30-2018 , 05:42   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #6

Plugin... Could you write it all over again?
SANTO37 is offline
Kushfield
Member
Join Date: Jan 2017
Location: Estonia
Old 06-30-2018 , 05:58   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #7

Quote:
Originally Posted by SANTO37 View Post
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
Kushfield is offline
SANTO37
Member
Join Date: Aug 2008
Old 06-30-2018 , 07:04   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #8

Excellent !
But I want the [say / steam] command to work.
You could ask me if you could.
I'm new.
SANTO37 is offline
SANTO37
Member
Join Date: Aug 2008
Old 06-30-2018 , 07:11   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #9

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.
SANTO37 is offline
Kushfield
Member
Join Date: Jan 2017
Location: Estonia
Old 06-30-2018 , 07:11   Re: [HELP] Saving Player steamid to SQL
Reply With Quote #10

Quote:
Originally Posted by SANTO37 View Post
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.

Last edited by Kushfield; 06-30-2018 at 07:15.
Kushfield 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 20:26.


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