[TUT]Bind plugin mysql

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

[TUT]Bind plugin mysql

Post by Archive-Poster »

Welcome all.
First of all, I apologize if something is not clear. I do not speak English and so I use a translator.

In this tutorial I will show you how to bind a plugin for mysql.

1)Attach include sqlx

Code: Select all

#include <sqlx>
2)Сreate arrays

Code: Select all

new Handle:MYSQL_Tuple //Formation of data base
new Handle:MYSQL_Connect //Connect to the database

new g_Cvar[5] //Cvars
new g_Access //Ip is there in the database
3)Create a function plugin_init()

Code: Select all

public plugin_init()
{
    register_plugin("[TUT]MysqlPegging", "1.0", "CrAsH")

    g_Cvar[1] = register_cvar("amx_pegging_hostname", "127.0.0.1") //hostname database
    g_Cvar[2] = register_cvar("amx_pegging_username", "root") //login database
    g_Cvar[3] = register_cvar("amx_pegging_password", "") //passsword database
    g_Cvar[4] = register_cvar("amx_pegging_database", "ip") //base database

    set_task(1.0, "MYSQL_Load") //Connect to the database
}
4)Create a connection to the database

Code: Select all

public MYSQL_Load()
{
    new szHostname[30], szUsername[30], szPassword[30], szDatabase[30] //Data
    new szError[512], szErr //Errors

    get_pcvar_string(g_Cvar[1], szHostname, charsmax( szHostname )) //we get hostname
    get_pcvar_string(g_Cvar[2], szUsername, charsmax( szUsername )) //we get login
    get_pcvar_string(g_Cvar[3], szPassword, charsmax( szPassword )) //we get password
    get_pcvar_string(g_Cvar[4], szDatabase, charsmax( szDatabase )) //we get dase

    MYSQL_Tuple = SQL_MakeDbTuple(szHostname, szUsername, szPassword, szDatabase) //prepare data
    MYSQL_Connect= SQL_Connect(MYSQL_Tuple, szErr, szError, charsmax( szError )) //Connect to the database

    if(MYSQL_Connect == Empty_Handle) //If the connection failed       
        set_fail_state( szError ) //Disable the plugin and displays the error

    set_task(1.0, "CheckIP") //Check the ip in the database
}
5)Check ip

Code: Select all

public CheckIP(id)
{
    new szError[512] //Error
    new szIP[32] //IP server
    new szMYSQLIP[32] //IP MYSQL

    new Handle:szSelect = SQL_PrepareQuery(MYSQL_Connect, "SELECT * FROM servers") //Make the calls to the database   

    if(!SQL_Execute( szSelect ))  //If the request failed 
    {
        SQL_QueryError(szSelect, szError, charsmax( szError )) //get an error
        set_fail_state( szError ) //Disable the plugin and displays the error
    }

    get_user_ip(id, szIP, charsmax( szIP ), 0) //get the ip of the server

    while(SQL_MoreResults( szSelect )) //Search the database   
    {
        SQL_ReadResult(szSelect, 1, szMYSQLIP, charsmax( szMYSQLIP )) //get ip from the database

        if(equal(szIP, szMYSQLIP)) //If there is a match
        {
            g_Access = true //allow access
            break //End the cycle
        }else{ //If a match is not
            SQL_NextRow(szSelect) //Check the following line
        }
    }

    if(!g_Access) //If access is denied
    {
        server_cmd("quit") //Turning off the server
    }
}
Ready version:

Code: Select all

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

new Handle:MYSQL_Tuple
new Handle:MYSQL_Connect

new g_Cvar[5]
new g_Access

public plugin_init()
{
    register_plugin("[TUT]MysqlPegging", "1.0", "CrAsH")

    g_Cvar[1] = register_cvar("amx_pegging_hostname", "127.0.0.1")
    g_Cvar[2] = register_cvar("amx_pegging_username", "root")
    g_Cvar[3] = register_cvar("amx_pegging_password", "vertrigo")
    g_Cvar[4] = register_cvar("amx_pegging_database", "ip")

    set_task(1.0, "MYSQL_Load")
}

public MYSQL_Load()
{
    new szHostname[30], szUsername[30], szPassword[30], szDatabase[30]
    new szError[512], szErr

    get_pcvar_string(g_Cvar[1], szHostname, charsmax( szHostname ))
    get_pcvar_string(g_Cvar[2], szUsername, charsmax( szUsername ))
    get_pcvar_string(g_Cvar[3], szPassword, charsmax( szPassword ))
    get_pcvar_string(g_Cvar[4], szDatabase, charsmax( szDatabase ))

    MYSQL_Tuple = SQL_MakeDbTuple(szHostname, szUsername, szPassword, szDatabase)
    MYSQL_Connect= SQL_Connect(MYSQL_Tuple, szErr, szError, charsmax( szError ))

    if(MYSQL_Connect == Empty_Handle)
        set_fail_state( szError )

    set_task(1.0, "CheckIP")
}

public CheckIP(id)
{
    new szError[512]
    new szIP[32]
    new szMYSQLIP[32]

    new Handle:szSelect = SQL_PrepareQuery(MYSQL_Connect, "SELECT * FROM servers")
   
    if(!SQL_Execute( szSelect ))   
    {
        SQL_QueryError(szSelect, szError, charsmax( szError ))
        set_fail_state( szError )
    }

    get_user_ip(id, szIP, charsmax( szIP ), 0)

    while(SQL_MoreResults( szSelect ))
    {
        SQL_ReadResult(szSelect, 1, szMYSQLIP, charsmax( szMYSQLIP ))

        if(equal(szIP, szMYSQLIP))
        {
            g_Access = true
            break
        }else{
            SQL_NextRow(szSelect)
        }
    }

    if(!g_Access)
    {
        server_cmd("quit")
    }
}
Structure of the base will be the

Code: Select all

CREATE TABLE IF NOT EXISTS `servers` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `ip_server` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
p.s Do not forget to put the right user mysql, it was impossible to add, update, data

Who is online

Users browsing this forum: No registered users and 1 guest