AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   sqlx help (https://forums.alliedmods.net/showthread.php?t=312394)

LondoN 12-01-2018 07:39

sqlx help
 
Code:

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

#define PLUGIN        "VIP SQL"
#define VERSION        "1.0"
#define AUTHOR        "LondoN eXtream"

// SQL Stuff
new g_CvarHost, g_CvarUser, g_CvarPass, g_CvarDB;
new g_pGlobalQuery [ 256 ];
new Handle: Tuple;

// VIP Stuff
new g_pName [ 33 ] [ 32 ];
new g_pPass [ 33 ] [ 32 ];
new bool: is_user_vip [ 33 ];

public plugin_precache ( )
{
        server_print ( "[VIP] Pluginul a pornit cu success" );

        g_CvarHost                =        register_cvar ( "vip_sqlx_host", "" );
        g_CvarUser                =        register_cvar ( "vip_sqlx_user", "" );
        g_CvarPass                =        register_cvar ( "vip_sqlx_pass", "" );
        g_CvarDB                =        register_cvar ( "vip_sqlx_db",        "" );

        SQLX_Connect ( );
}
       
public plugin_init ( )
{
        register_plugin ( PLUGIN, VERSION, AUTHOR );
        register_cvar ( "vip_sqlx_london", VERSION, FCVAR_SERVER|FCVAR_SPONLY );
        set_cvar_string ( "vip_sqlx_london", VERSION );

}

public SQLX_Connect ( )
{
        new szHost [ 32 ], szUser [ 32 ], szPass [ 32 ], szDB [ 32 ];
        get_pcvar_string ( g_CvarHost, szHost, charsmax ( szHost ) );
        get_pcvar_string ( g_CvarUser, szUser, charsmax ( szUser ) );
        get_pcvar_string ( g_CvarPass, szPass, charsmax ( szPass ) );
        get_pcvar_string ( g_CvarDB, szDB, charsmax ( szDB ) );

        Tuple = SQL_MakeDbTuple ( szHost, szUser, szPass, szDB, 10 );
}

public client_connect ( id )
{
        if ( is_user_bot ( id ) || is_user_hltv ( id ) )
                return;

        get_user_name ( id, g_pName [ id ], charsmax ( g_pName ) );
        get_user_info ( id, "_pass", g_pPass [ id ], charsmax ( g_pPass ) );
        formatex ( g_pGlobalQuery, charsmax ( g_pGlobalQuery ), "SELECT * FROM vip_users" );
        SQL_ThreadQuery ( Tuple, "ReadInfo", g_pGlobalQuery );
}

public ReadInfo ( FailState, Handle: Query, szError, Errcode, Data [ ], DataSize )
{
        if ( FailState == TQUERY_CONNECT_FAILED )
        {
                log_amx ( "Nu ne putem conecta la %d [%s]", Errcode, szError );
                return;
        }
        else if ( FailState == TQUERY_QUERY_FAILED )
        {
                log_amx ( "Query Error: %d [%s]", Errcode, szError );
                return;
        }

        if ( SQL_NumResults ( Query ) > 1 )
        {
                new Player = Data [ 0 ];
                new TempUser [ 32 ], TempPass [ 32 ];
                SQL_ReadResult ( Query, 1, TempUser, charsmax ( TempUser ) );
                SQL_ReadResult ( Query, 2, TempPass, charsmax ( TempPass ) );

                if ( equal ( g_pName [ Player ], TempUser ) )
                {
                        if ( equal ( g_pPass [ Player ], TempPass ) )
                        {
                                is_user_vip [ Player ] = true;
                                server_print ( "[VIP] %s connected with success", g_pName [ Player ] );
                        }
                        else
                        {
                                server_cmd ( "kick #%d", get_user_userid ( Player ) );
                                server_print ( "[VIP] %s Kicked due to Invalid Password", g_pName [ Player ] );
                        }
                }
        }
}

i want to retrive user and pass from database, i have some error, can someone explain me what is wrong with the code?

Bugsy 12-01-2018 10:36

Re: sqlx help
 
1. You should always show which error you are getting when asking for help. Since you didn't, I need to look at your entire plugin instead of looking for something specific. Enable debug mode in plugins.ini when developing your plugin.
2. Is login the only SQL query you are using ReadInfo() to process? If so then you are ok, if not then you need to do additional work to make it know which transaction you are doing.
3. You are sending a query to get all records from vip_users and then assuming the first one returned is the player you are looking for. You should specify additional conditions in your query to get data for only the player you want. I am not sure what field names you are using, replace 'username' and 'pwd' accordingly. Also, you should be sending steam-id, not name since any player can use any name, you cannot base security off of a name.

Untested
Code:
#include < amxmodx > #include < amxmisc > #include < sqlx > #define PLUGIN  "VIP SQL" #define VERSION "1.0" #define AUTHOR  "LondoN eXtream" // SQL Stuff new g_CvarHost, g_CvarUser, g_CvarPass, g_CvarDB; new g_pGlobalQuery [ 256 ]; new Handle: Tuple; // VIP Stuff new g_pName [ 33 ] [ 32 ]; new g_pPass [ 33 ] [ 32 ]; new bool: is_user_vip [ 33 ]; public plugin_precache ( ) {     server_print ( "[VIP] Pluginul a pornit cu success" );     g_CvarHost      =   register_cvar ( "vip_sqlx_host", "35.190.179.72" );     g_CvarUser      =   register_cvar ( "vip_sqlx_user", "gamezeer_vip" );     g_CvarPass      =   register_cvar ( "vip_sqlx_pass", "undercoveR123" );     g_CvarDB        =   register_cvar ( "vip_sqlx_db""gamezeer_vip" );     SQLX_Connect ( ); }     public plugin_init ( ) {     register_plugin ( PLUGIN, VERSION, AUTHOR );     register_cvar ( "vip_sqlx_london", VERSION, FCVAR_SERVER|FCVAR_SPONLY );     set_cvar_string ( "vip_sqlx_london", VERSION ); } public SQLX_Connect ( ) {     new szHost [ 32 ], szUser [ 32 ], szPass [ 32 ], szDB [ 32 ];     get_pcvar_string ( g_CvarHost, szHost, charsmax ( szHost ) );     get_pcvar_string ( g_CvarUser, szUser, charsmax ( szUser ) );     get_pcvar_string ( g_CvarPass, szPass, charsmax ( szPass ) );     get_pcvar_string ( g_CvarDB, szDB, charsmax ( szDB ) );     Tuple = SQL_MakeDbTuple ( szHost, szUser, szPass, szDB, 10 ); } public client_connect ( id ) {     if ( is_user_bot ( id ) || is_user_hltv ( id ) )         return;     get_user_name ( id, g_pName [ id ], charsmax ( g_pName ) );     get_user_info ( id, "_pass", g_pPass [ id ], charsmax ( g_pPass ) );         //This query is getting every player in vip_users when you only want data for 1 player.     //You technically can do it this way, but then you would need to cycle through every result in your query handler using SQL_NextRow(), which is inefficient.     //formatex ( g_pGlobalQuery, charsmax ( g_pGlobalQuery ), "SELECT * FROM vip_users" );         //Query for records for those that have matching name and password only.     formatex ( g_pGlobalQuery, charsmax ( g_pGlobalQuery ), "SELECT * FROM vip_users WHERE username=^"%s^" AND pwd=^"%s^"" , g_pName [ id ] , g_pPass [ id ] );         //In your query handler, you are assuming Data[0] holds the player ID but you are never setting this before executing the query. This results in player id always being 0 in the handler.     //SQL_ThreadQuery( Tuple, "ReadInfo", g_pGlobalQuery );     //This is where to can specify the data passed in Data[] so that it can be used in the query result.     new queryData[ 1 ];     queryData[ 0 ] = id;     SQL_ThreadQuery( Tuple, "ReadInfo", g_pGlobalQuery , queryData , sizeof( queryData ) ); } public ReadInfo ( FailState, Handle: Query, szError, Errcode, Data [ ], DataSize ) {     if ( FailState == TQUERY_CONNECT_FAILED )     {         log_amx ( "Nu ne putem conecta la %d [%s]", Errcode, szError );         return;     }     else if ( FailState == TQUERY_QUERY_FAILED )     {         log_amx ( "Query Error: %d [%s]", Errcode, szError );         return;     }     new Player = Data [ 0 ];         //This should be a check for > 0 instead of > 1     //if ( SQL_NumResults ( Query ) > 1 )     if ( SQL_NumResults ( Query ) > 0 )     {         //new TempUser [ 32 ];         //new TempPass [ 32 ];                 //SQL_ReadResult ( Query, 1, TempUser, charsmax ( TempUser ) );         //SQL_ReadResult ( Query, 2, TempPass, charsmax ( TempPass ) );         //if ( equal ( g_pName [ Player ], TempUser ) )         //{         //if ( equal ( g_pPass [ Player ], TempPass ) )         //{             is_user_vip [ Player ] = true;             server_print ( "[VIP] %s connected with success", g_pName [ Player ] );         //}         //else         //{         //  server_cmd ( "kick #%d", get_user_userid ( Player ) );         //  server_print ( "[VIP] %s Kicked due to Invalid Password", g_pName [ Player ] );         //}         //}     }     else     {         //No records returned which means there was no username + password match         server_cmd ( "kick #%d", get_user_userid ( Player ) );         server_print ( "[VIP] %s Kicked due to Invalid Password", g_pName [ Player ] );     }         }

Natsheh 12-01-2018 11:43

Re: sqlx help
 
Have you tried to read the documentary everything there is well explained!

Bugsy 12-01-2018 11:54

Re: sqlx help
 
I don't think his coding was terribly bad, just the wrong process/flow was used.

LondoN 12-01-2018 15:29

Re: sqlx help
 
I've tested bugsy's code but it give me one more problem, it did not retrive the player id in ReadInfo().

debug in console

Code:

[VIP SQLx] Forward "client_connect" called
[VIP SQLx] Query retriving vip information have executed
[VIP SQLx] No error found during execute SQL Query
[VIP SQLx]  kicked due to Invalid Password

last line must be [VIP SQLx] %s (name) kicked due to Invalid Password

Bugsy 12-01-2018 15:38

Re: sqlx help
 
Not sure, the player id is definitely getting passed.

Try changing

PHP Code:

public ReadInfo FailStateHandleQueryszErrorErrcodeData [ ], DataSize )

to 

public ReadInfo FailStateHandleQueryszError[], ErrcodeData [ ], DataSize 

Also add a server print to confirm player is passed
Code:
new Player = Data [ 0 ]; server_print( "Player id = %d" , Player );

Natsheh 12-01-2018 16:00

Re: sqlx help
 
Where are you creating the table ? Show your sql table creation syntax

Also remove your mysql database information....

LondoN 12-01-2018 17:32

Re: sqlx help
 
Code:

CREATE TABLE vip_users (
        username varchar(32),
        password varchar(32)
);

sql

Natsheh 12-01-2018 18:03

Re: sqlx help
 
Can you provide it with amx code..

I cant help you if you are not providing enough code.

#bugsy also it shouldnt kick the user if nothing was found in database. Atleast it should be confirmed that the user is a vip by auth and his password is invalid.

Bugsy 12-01-2018 18:49

Re: sqlx help
 
Quote:

Originally Posted by LondoN (Post 2626308)
Code:

CREATE TABLE vip_users (
        username varchar(32),
        password varchar(32)
);

sql

Quote:

Originally Posted by Natsheh (Post 2626311)
Can you provide it with amx code..

I cant help you if you are not providing enough code.

#bugsy also it shouldnt kick the user if nothing was found in database. Atleast it should be confirmed that the user is a vip by auth and his password is invalid.

I think what he provided shows his table has 2 columns, username and password.

And yeah, that was my bad.

@OP, try the below
PHP Code:

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

#define PLUGIN    "VIP SQL"
#define VERSION    "1.0"
#define AUTHOR    "LondoN eXtream"

// SQL Stuff
new g_CvarHostg_CvarUserg_CvarPassg_CvarDB;
new 
g_pGlobalQuery 256 ];
new 
HandleTuple;

// VIP Stuff
new g_pName 33 ] [ 32 ];
new 
g_pPass 33 ] [ 32 ];
new 
boolis_user_vip 33 ];

public 
plugin_precache ( )
{
    
server_print "[VIP] Pluginul a pornit cu success" );

    
g_CvarHost register_cvar "vip_sqlx_host""35.190.179.72" );
    
g_CvarUser register_cvar "vip_sqlx_user""gamezeer_vip" );
    
g_CvarPass register_cvar "vip_sqlx_pass""undercoveR123" );
    
g_CvarDB register_cvar "vip_sqlx_db",    "gamezeer_vip" );

    
SQLX_Connect ( );
}
    
public 
plugin_init ( )
{
    
register_plugin PLUGINVERSIONAUTHOR );
    
register_cvar "vip_sqlx_london"VERSIONFCVAR_SERVER|FCVAR_SPONLY );
    
set_cvar_string "vip_sqlx_london"VERSION );

}

public 
SQLX_Connect ( )
{
    new 
szHost 32 ], szUser 32 ], szPass 32 ], szDB 32 ];
    
get_pcvar_string g_CvarHostszHostcharsmax szHost ) );
    
get_pcvar_string g_CvarUserszUsercharsmax szUser ) );
    
get_pcvar_string g_CvarPassszPasscharsmax szPass ) );
    
get_pcvar_string g_CvarDBszDBcharsmax szDB ) );

    
Tuple SQL_MakeDbTuple szHostszUserszPassszDB10 );
}

public 
client_connect id )
{
    if ( 
is_user_bot id ) || is_user_hltv id ) )
        return;

    
get_user_name idg_pName id ], charsmax g_pName ) );
    
get_user_info id"_pass"g_pPass id ], charsmax g_pPass ) );
    
    
//This query is getting every player in vip_users when you only want data for 1 player.
    //You technically can do it this way, but then you would need to cycle through every result in your query handler using SQL_NextRow(), which is inefficient.
    //formatex ( g_pGlobalQuery, charsmax ( g_pGlobalQuery ), "SELECT * FROM vip_users" );
    
    //Query for records for those that have matching name and password only.
    
formatex g_pGlobalQuerycharsmax g_pGlobalQuery ), "SELECT password FROM vip_users WHERE username=^"%s^"" g_pName id ] );
    
    
//In your query handler, you are assuming Data[0] holds the player ID but you are never setting this before executing the query. This results in player id always being 0 in the handler.
    //SQL_ThreadQuery( Tuple, "ReadInfo", g_pGlobalQuery );

    //This is where to can specify the data passed in Data[] so that it can be used in the query result.
    
new queryData];
    
queryData] = id;
    
SQL_ThreadQueryTuple"ReadInfo"g_pGlobalQuery queryData sizeofqueryData ) );
}

public 
ReadInfo FailStateHandleQueryszErrorErrcodeData [ ], DataSize )
{
    if ( 
FailState == TQUERY_CONNECT_FAILED )
    {
        
log_amx "Nu ne putem conecta la %d [%s]"ErrcodeszError );
        return;
    }
    else if ( 
FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx "Query Error: %d [%s]"ErrcodeszError ); 
        return;
    }

    new 
Player Data ];
    
    
//This should be a check for > 0 instead of > 1
    //if ( SQL_NumResults ( Query ) > 1 )
    
if ( SQL_NumResults Query ) )
    {
        
//new TempUser [ 32 ];
        
new TempPass 32 ];
        
        
//SQL_ReadResult ( Query, 1, TempUser, charsmax ( TempUser ) );
        
SQL_ReadResult Query0TempPasscharsmax TempPass ) );

        
//if ( equal ( g_pName [ Player ], TempUser ) )
        //{
        
if ( equal g_pPass Player ], TempPass ) )
        {
            
is_user_vip Player ] = true;
            
server_print "[VIP] %s connected with success"g_pName Player ] );
        }
        else
        {
            
//User found but password did not match
            
server_cmd "kick #%d"get_user_userid Player ) );
            
server_print "[VIP] %s Kicked due to Invalid Password"g_pName Player ] );    
        }

        
//}
    
}    
    else
    {
        
//player not found in table
    
}




All times are GMT -4. The time now is 07:33.

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