[Include] Easy MySQL - Simplifying the usage of MySQL queries!
#1

Easy - MySQL [V3.8]


Originally, created by ThePhenix (ThreeKingz), currently maintaining and developing this include.

Old thread:


Overview:

This include allows you to create and manage several MySQL queries in a very simplified way, it means that you won't require to write queries on your own for the most part.


Login/Register example:


PHP Code:
#include <a_samp>
#include <easy-mysql>
/*
 *  Simple register/login system using easy_mysql.inc!
*/
#define mysql_host    "localhost"
#define mysql_user    "root"
#define mysql_db        "server"
#define mysql_pass         ""
#define mysql_debugging_enabled             (true)
#define SQL_PLAYERS_TABLE                    "players"
#define DIALOG_LOGIN 0
#define DIALOG_REGISTER 1
enum p_info
{
    
p_id,
    
p_name[24],
    
p_password[64],
    
p_score,
    
Float:p_posx,
    
Float:p_posy,
    
Float:p_posz,
    
p_loggedin
};
new 
UserInfo[MAX_PLAYERS][p_info];
    
stock ret_pName(playerid)
{
    new 
name[24];
    
GetPlayerName(playeridnamesizeof(name));
    return 
name;
}
#define IsPlayerRegistered(%0)    (SQL::RowExistsEx(SQL_PLAYERS_TABLE, "p_name", ret_pName(%0)))
main()
{
    
}
public 
OnGameModeInit()
{
    
//Connecting to the MySQL database, default connection handle.
    
SQL::Connect(mysql_hostmysql_usermysql_passmysql_db);
    
//Checking if the table 'players' exists
    
    //Checking if the table 'players' exists
    
if(!SQL::ExistsTable(SQL_PLAYERS_TABLE))
    {
        
//If not, then create a table called 'players'.
        
new handle SQL::Open(SQL::CREATESQL_PLAYERS_TABLE); //Opening a valid handle to create a table called 'players'
        
SQL::AddTableColumn(handle"p_id"SQL_TYPE_INT11falsetrue); //Setting auto-increment for this field.
        
SQL::AddTableColumn(handle"p_name"SQL_TYPE_VCHAR24);
        
SQL::AddTableColumn(handle"p_password"SQL_TYPE_VCHAR64);
        
SQL::AddTableColumn(handle"p_score"SQL_TYPE_INT);
        
SQL::AddTableColumn(handle"p_posx"SQL_TYPE_FLOAT);
        
SQL::AddTableColumn(handle"p_posy"SQL_TYPE_FLOAT);
        
SQL::AddTableColumn(handle"p_posz"SQL_TYPE_FLOAT);
        
SQL::Close(handle);//Closing the previous opened handle.
    
}
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
UserInfo[playerid][p_loggedin] = 0UserInfo[playerid][p_score] = 0;  UserInfo[playerid][p_posx] = 1958.3783;
    
UserInfo[playerid][p_posy] = 1343.1572UserInfo[playerid][p_posz] = 15.3746
    if(
SQL::RowExistsEx(SQL_PLAYERS_TABLE"p_name"ret_pName(playerid))) //Check if the name is registered in the database
    
{
        
//Get the player password and unique ID.
        
new handle SQL::OpenEx(SQL::READ"players""p_name"ret_pName(playerid));
        
SQL::ReadString(handle"p_password"UserInfo[playerid][p_password], 64);
        
SQL::ReadInt(handle"p_id"UserInfo[playerid][p_id]);
        
SQL::Close(handle);
        
//Show the login dialog
        
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"{0080FF}Login""Please input your password below to log in.""Login""Exit");
    }
    else
    {
        
//If not registered, then show the register DIALOG.
        
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"{0080FF}Register""Please input a password below to register in.""Login""Exit");
    }
    return 
1;
}
public 
OnPlayerSpawn(playerid)
{
    
SetPlayerPos(playeridUserInfo[playerid][p_posx], UserInfo[playerid][p_posy], UserInfo[playerid][p_posz]);
    return 
1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    if(
UserInfo[playerid][p_loggedin] == 1)
    {
        
//Save the player data.
        
GetPlayerPos(playeridUserInfo[playerid][p_posx], UserInfo[playerid][p_posy], UserInfo[playerid][p_posz]);
        new 
handle SQL::Open(SQL::UPDATESQL_PLAYERS_TABLE"p_id"UserInfo[playerid][p_id]); //update WHERE p_id = UserInfo[playerid][p_id]
        
SQL::WriteInt(handle"p_score"GetPlayerScore(playerid));
        
SQL::WriteFloat(handle"p_posx"UserInfo[playerid][p_posx]);
        
SQL::WriteFloat(handle"p_posy"UserInfo[playerid][p_posy]);
        
SQL::WriteFloat(handle"p_posz"UserInfo[playerid][p_posz]);
        
SQL::Close(handle);
    }
    return 
1;
}
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_REGISTER:
        {
            if(!
response) return Kick(playerid);
            if(
strlen(inputtext) < 5)
            {
                
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"{0080FF}Register""Please input a password below to register in.""Login""Exit");
                return 
1;
            }
            
SHA256_PassHash(inputtext""UserInfo[playerid][p_password], 64);
            new 
handle SQL::Open(SQL::INSERTSQL_PLAYERS_TABLE);
            
SQL::ToggleAutoIncrement(handletrue);//Toggles auto increment, SQL::Close will return cache_insert_id();
            
SQL::WriteString(handle"p_name"ret_pName(playerid));
            
SQL::WriteString(handle"p_password"UserInfo[playerid][p_password]);
            
SQL::WriteInt(handle"p_score"0);
            
SQL::WriteFloat(handle"p_posx"0.0);
            
SQL::WriteFloat(handle"p_posy"0.0);
            
SQL::WriteFloat(handle"p_posz"0.0);
            
UserInfo[playerid][p_id] = SQL::Close(handle); 
            
SendClientMessage(playerid, -1"Successfully registered in!");
            
UserInfo[playerid][p_loggedin] = 1;
        }
        case 
DIALOG_LOGIN:
        {
            if(!
responseKick(playerid); 
            new 
hash[64];
            
SHA256_PassHash(inputtext""hash64);
            if(!
strcmp(hashUserInfo[playerid][p_password]))
            { 
                
//Load player data
                
new handle SQL::Open(SQL::READSQL_PLAYERS_TABLE"p_id"UserInfo[playerid][p_id]);
                
SQL::ReadInt(handle"p_score"UserInfo[playerid][p_score]);
                
SQL::ReadFloat(handle"p_posx"UserInfo[playerid][p_posx]);
                
SQL::ReadFloat(handle"p_posy"UserInfo[playerid][p_posy]);
                
SQL::ReadFloat(handle"p_posz"UserInfo[playerid][p_posz]);
                
SQL::Close(handle);//You must close the handle.
                
SetPlayerScore(playeridUserInfo[playerid][p_score]);
                
UserInfo[playerid][p_loggedin] = 1;
                
SendClientMessage(playerid, -1"Successfully logged in!");
                
            }
            else 
            {
                
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"{0080FF}Login""Please input your password below to log in.""Login""Exit");
            }
        }
    }
    return 
1;

Credits:

- ThePhenix (ThreeKingz) (initial developer)
- Max_Andolini (latest developer)

----------------------------------------------------------------------------------------------------------------------
Download:

Download V3.8

The latest MySQL plugin (R41-4) is required to use this include.


If you have any questions or suggestions, comment below.
Reply


Messages In This Thread
Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 11.05.2016, 16:16
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Evoturk - 11.05.2016, 16:18
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Unrea1 - 11.05.2016, 16:22
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 11.05.2016, 16:25
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Sensation - 11.05.2016, 18:40
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 11.05.2016, 18:41
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Sensation - 11.05.2016, 18:42
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Sensation - 11.05.2016, 18:49
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Luicy. - 19.06.2016, 08:22
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 19.06.2016, 10:22
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by pawnuser - 21.06.2016, 05:30
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 21.06.2016, 10:21
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by pawnuser - 21.06.2016, 10:58
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 21.06.2016, 13:30
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by pawnuser - 21.06.2016, 15:50
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 21.06.2016, 16:47
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by pawnuser - 21.06.2016, 17:07
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Stones - 21.06.2016, 19:45
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 05.09.2016, 21:23
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by morris91 - 06.09.2016, 11:18
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 06.09.2016, 11:23
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by morris91 - 06.09.2016, 11:25
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 06.09.2016, 12:16
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by F1N4L - 06.09.2016, 12:21
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by morris91 - 06.09.2016, 12:52
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 15.09.2016, 15:37
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by vernz - 07.11.2016, 01:30
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by GeneralAref - 19.12.2016, 10:51
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 19.12.2016, 12:09
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Zoie - 04.04.2017, 09:17
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 24.11.2017, 17:36
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by RowdyrideR - 10.01.2018, 15:37
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 10.01.2018, 15:41
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by RowdyrideR - 11.01.2018, 01:07
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by ThePhenix - 11.01.2018, 01:55
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by RowdyrideR - 11.01.2018, 17:35
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by ThePhenix - 11.01.2018, 17:45
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by RowdyrideR - 11.01.2018, 18:26
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 13.05.2018, 16:24
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Private200 - 13.05.2018, 20:58
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 13.05.2018, 21:00
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Private200 - 13.05.2018, 21:08
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 13.05.2018, 21:36
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Akeem - 06.06.2018, 15:21
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 06.06.2018, 16:25
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by altunatively - 14.06.2018, 19:30
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 23.06.2018, 13:46
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 23.06.2018, 13:49
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 23.06.2018, 14:14
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 23.06.2018, 14:21
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 23.06.2018, 14:26
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 23.06.2018, 14:56
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by CantBeJohn - 17.09.2018, 04:34
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 17.09.2018, 16:34
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by CantBeJohn - 18.09.2018, 01:52
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Amagida - 21.09.2018, 16:23
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by solstice_ - 21.09.2018, 17:25
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 29.09.2018, 12:10
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by GameOvr - 12.10.2018, 07:55
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by CantBeJohn - 09.07.2019, 19:24
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Gammix - 09.07.2019, 21:45
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Aerotactics - 17.07.2019, 03:35
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 18.05.2020, 19:48
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 19.05.2020, 12:02
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 20.05.2020, 01:24
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Stefan_Toretto - 22.05.2020, 20:57
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by Max_Andolini - 01.06.2020, 01:03
Re: Easy MySQL - Simplifying the usage of MySQL queries! - by JR_Junior - 23.06.2020, 22:30

Forum Jump:


Users browsing this thread: 1 Guest(s)