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

[TUT]Bind plugin mysql


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrAsH.94
Junior Member
Join Date: Feb 2013
Old 02-25-2013 , 20:37   [TUT]Bind plugin mysql
Reply With Quote #1

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:
#include <sqlx>
2)Сreate arrays

Code:
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:
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:
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:
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:
#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:
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

Last edited by CrAsH.94; 02-25-2013 at 20:39.
CrAsH.94 is offline
KORD_12.7
Senior Member
Join Date: Aug 2009
Location: Russia, Vladivostok
Old 02-26-2013 , 00:59   Re: [TUT]Bind plugin mysql
Reply With Quote #2

And what a point of this? Sell private plugins? Anyway, it's too easy to bypass your checks.
__________________

Vi Veri Veniversum Vivus Vici
Russian Half-Life and Adrenaline Gamer community
KORD_12.7 is offline
Send a message via ICQ to KORD_12.7
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 02-26-2013 , 13:45   Re: [TUT]Bind plugin mysql
Reply With Quote #3

Yep, admins can read the CVars, make them Variables in the plugin inside only.
__________________
Kia is offline
aaarnas
Veteran Member
Join Date: Jun 2008
Location: Lithuania
Old 02-26-2013 , 13:49   Re: [TUT]Bind plugin mysql
Reply With Quote #4

It's totally wrong. You even not using thread query.
__________________
My plugins:
[ZP] ZM_VIP (v2.0.0 is comming. v1.9.1 BETA is out!)

aaarnas is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 03-02-2013 , 08:32   Re: [TUT]Bind plugin mysql
Reply With Quote #5

Quote:
You even not using thread query.
How does that make it wrong?
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
aaarnas
Veteran Member
Join Date: Jun 2008
Location: Lithuania
Old 03-04-2013 , 22:56   Re: [TUT]Bind plugin mysql
Reply With Quote #6

Quote:
Originally Posted by Backstabnoob View Post
How does that make it wrong?
http://en.wikipedia.org/wiki/Thread_(computing)

For long time taking queries there should be sort of lag or longer map change if executing on plugin_init or etc. It depends on some chases.
__________________
My plugins:
[ZP] ZM_VIP (v2.0.0 is comming. v1.9.1 BETA is out!)


Last edited by aaarnas; 03-04-2013 at 23:00.
aaarnas is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 03-09-2013 , 05:42   Re: [TUT]Bind plugin mysql
Reply With Quote #7

That still doesn't make it wrong to use it. There are times where normal queries are better than threaded queries.

Edit: I don't want to look like I approve this "tutorial". It's completely useless and stupid, because:

1) You shouldn't even send your plugins without source. Some people would like to change a few things to match their needs. Being forced to use something that doesn't work right on a certain mod is retarded. There is nothing wrong with selling plugins, but damn, at least include the source code.

2) Anyone could access your database. It's not really hard to disassemble the amxx file and see this:

Code:
.DATA
    arr_000 array 8 fill 0x0
    str_000 string "[TUT]MysqlPegging"
    str_001 string "1.0"
    str_002 string "CrAsH"
    str_003 string "amx_pegging_hostname"
    str_004 string "127.0.0.1"
    str_005 string "amx_pegging_username"
    str_006 string "root"
    str_007 string "amx_pegging_password"
    str_008 string "vertrigo"
    str_009 string "amx_pegging_database"
    str_010 string "ip"
    str_011 string "MYSQL_Load"
    arr_001 array 2 fill 0x0
    str_012 string "CheckIP"
    str_013 string "SELECT * FROM servers"
    str_014 string "quit"
Which renders the plugin completely useless.
__________________
Currently busy working on a very large scale anime database project.

Last edited by Backstabnoob; 03-09-2013 at 05:54.
Backstabnoob is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 03-09-2013 , 12:05   Re: [TUT]Bind plugin mysql
Reply With Quote #8

1) providing the source code is required, no exceptions. So this type of "block" servers no point since anyone will be able to remove it

2) since they are cvars, you don't even need to disassemble the plugin to get the cause or even change them. The command "amxx cvars" will list them
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
BlackSHot
Junior Member
Join Date: Aug 2012
Old 11-12-2013 , 16:38   Re: [TUT]Bind plugin mysql
Reply With Quote #9

Can someone make the information for host, db, password, username without cvar's?
BlackSHot is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 11-12-2013 , 19:39   Re: [TUT]Bind plugin mysql
Reply With Quote #10

Quote:
Originally Posted by BlackSHot View Post
Can someone make the information for host, db, password, username without cvar's?

hahahah it is a complete joke!

When you dissasemble any compiled .amxx file, this will show the strings in compiled plugin.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
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 10:40.


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