Raised This Month: $ Target: $400
 0% 

Check if something exists in MySQL Database


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Croxeye
Member
Join Date: Jul 2015
Old 03-08-2016 , 14:09   Check if something exists in MySQL Database
Reply With Quote #1

Can anyone create full code who check if something exists in mysql database ?

Code:
..."SELECT 1 FROM `my_table` WHERE `Something` = '%s", Something);
Code:
if(SQL_NumResults(hQuery) < 1) { 
    server_print("Not exists"); 
} 
else { 
    server_print("Exists"); 
}
I need full code to do this.
__________________
Croxeye is offline
Old.School
Senior Member
Join Date: Sep 2015
Location: France
Old 03-08-2016 , 14:32   Re: Check if something exists in MySQL Database
Reply With Quote #2

this is scripting help section not plugins Request
__________________
You keep bringing B.R down .. He will rise again and kick Enemies asses !
Old.School is offline
Croxeye
Member
Join Date: Jul 2015
Old 03-08-2016 , 15:02   Re: Check if something exists in MySQL Database
Reply With Quote #3

I need some help about this code..
__________________
Croxeye is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-08-2016 , 18:06   Re: Check if something exists in MySQL Database
Reply With Quote #4

Quote:
I need some help about this code..
We understand that, but you need to be specific. Do you have an entire plugin and something is not working correctly? Are you trying to make a plugin that already uses SQL and you cant figure this part out? Are you just starting a plugin and know nothing about SQL?
__________________

Last edited by Bugsy; 03-08-2016 at 18:10.
Bugsy is offline
Old.School
Senior Member
Join Date: Sep 2015
Location: France
Old 03-08-2016 , 18:29   Re: Check if something exists in MySQL Database
Reply With Quote #5

i think i know what he want he need an exemple to use it on another code
__________________
You keep bringing B.R down .. He will rise again and kick Enemies asses !
Old.School is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-08-2016 , 18:56   Re: Check if something exists in MySQL Database
Reply With Quote #6

Read and learn here: https://forums.alliedmods.net/showthread.php?t=46779
__________________
Bugsy is offline
Croxeye
Member
Join Date: Jul 2015
Old 03-13-2016 , 15:50   Re: Check if something exists in MySQL Database
Reply With Quote #7

Please I need help about this:

Code:
public type_pw(id) 
{ 
    new typed_pw[32] 
     
    read_args(typed_code, charsmax(typed_pw)) 
    remove_quotes(typed_pwe) 

    if(equal(typed_pw, "")) 
    { 
        return PLUGIN_HANDLED; 
    } 
    else 
    { 
        if(SQL_NumResults(Query) < 1) 
        { 
            //Not Exists
        } 
        else 
        { 
            //Exists
        }   
    } 
    return PLUGIN_HANDLED; 
}
HOW TO MAKE QUERY AND CHECK IF VALUE EXISTS IN MYSQL DATABASE (+ MySQL Connection)
__________________
Croxeye is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-13-2016 , 18:08   Re: Check if something exists in MySQL Database
Reply With Quote #8

Will need more info. Should the password be tied to the players name? This will only check if the password exists in passwords table.

database name=server
table name=tblPasswords
field name=PW
PHP Code:
#include <amxmodx>
#include <sqlx>

new const Version[] = "0.1";

new 
Handle:g_SQLTuple;

public 
plugin_init() 
{
    
register_plugin"Check Password" Version "bugsy" );
    
    
register_concmd"login" "type_pw" );
}

public 
plugin_cfg()
{
    
SQL_SetAffinity"sqlite" );
    
g_SQLTuple SQL_MakeDbTuple"" "" "" "server" );
}

public 
type_pw(id

    new 
typed_pw32 ];
    
    if ( !
read_argstyped_pw charsmaxtyped_pw ) ) )    
        return 
PLUGIN_HANDLED;
        
    
remove_quotes(typed_pw

    new 
Handle:SQLConnection iErrorCode szError32 ];
    
SQLConnection SQL_Connectg_SQLTuple iErrorCode szError charsmaxszError ) );
    
    if( 
SQLConnection == Empty_Handle 
        
set_fail_stateszError );
        
    new 
Handle:Query SQL_PrepareQuerySQLConnection "SELECT 1 FROM tblPasswords WHERE PW='%s'" typed_pw );
    
    if( !
SQL_ExecuteQuery ) )
    {
        
SQL_QueryErrorQuery szError charsmaxszError ) );
        
set_fail_stateszError );
    }
    else
    {
        if ( 
SQL_NumResultsQuery ) > )
        {
            
client_printid print_chat "Password correct" );
        }
        else
        {
            
client_printid print_chat "Password incorrect" );
        }
    }
    
    return 
PLUGIN_HANDLED

__________________

Last edited by Bugsy; 03-13-2016 at 18:15.
Bugsy is offline
Old 03-13-2016, 18:52
Croxeye
This message has been deleted by Croxeye.
Croxeye
Member
Join Date: Jul 2015
Old 03-13-2016 , 19:07   Re: Check if something exists in MySQL Database
Reply With Quote #9

I just edit some thing:

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

new const Version[] = "0.1";
new 
Host[]     = ""
new User[]    = ""
new Pass[]     = ""
new Db[]     = ""


new Handle:g_SQLTuple g_Error[512], g_szQueryCache128 ];

public 
plugin_init() 
{
    
register_plugin"Check Password" Version "bugsy" );
    
    
register_concmd"login" "type_pw" );

    
set_task(1.0"MySQL_Init")
}

public 
MySQL_Init()
{
    
g_SQLTuple SQL_MakeDbTuple(Host,User,Pass,Db)

    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SQLTuple,ErrorCode,g_Error,charsmax(g_Error))
    
    if(
SqlConnection == Empty_Handle)
        
set_fail_state(g_Error)
    
    
SQL_FreeHandle(SqlConnection)   
}

public 
type_pw(id

    new 
typed_pw32 ];
    
    if ( !
read_argstyped_pw charsmaxtyped_pw ) ) )    
        return 
PLUGIN_HANDLED;
        
    
remove_quotes(typed_pw

    if( 
equaltyped_pw "" ) ) 
    { 
        return 
PLUGIN_HANDLED
    } 
    else 
    {
        new 
Handle:SQLConnection iErrorCode szError32 ];
        
SQLConnection SQL_Connectg_SQLTuple iErrorCode szError charsmaxszError ) );
        
        if( 
SQLConnection == Empty_Handle 
            
set_fail_stateszError );
            
        new 
Handle:Query SQL_PrepareQuerySQLConnection "SELECT 1 FROM tblPasswords WHERE PW='%s'" typed_pw );
        
        if( !
SQL_ExecuteQuery ) )
        {
            
SQL_QueryErrorQuery g_szQueryCache 100 );
            
set_fail_stateg_szQueryCache );
        }
        
        if ( 
SQL_NumResultsQuery ) > )
        {
            
client_printid print_chat "Password correct" );
        }
        else
        {
            
client_printid print_chat "Password incorrect" );
        }
    }
    
    return 
PLUGIN_HANDLED

Code:
L 03/14/2016 - 00:18:00: [AMXX] Plugin says: Can't connect to MySQL server on 'my_host' (110)
I can compile it without errors but I can't connect to mysql database. But when I try with PHP (With same user,host,pw,db) it works..
__________________
Croxeye is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-13-2016 , 19:10   Re: Check if something exists in MySQL Database
Reply With Quote #10

I'm not sure about how to resolve your connectivity issues.

Why are these empty?
PHP Code:
new Host[]     = ""
new User[]    = ""
new Pass[]     = ""
new Db[]     = "" 
__________________
Bugsy 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 07:03.


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