13.05.2013, 12:49
(
Last edited by Knappen; 15/09/2013 at 11:38 AM.
)
Introduction
I've been on these forums for a while, and I keep seeing people struggling to use the MySQL plugin by G-Stylez/BlueG. MySQL is hard to use, if you don't know HOW to use it. That's why I decided to create a small include with functions for loading and saving playerdata. I've made it as simple as possible. I haven't tested it with several players yet, but it should work pretty good.
This include uses the latest version of MySQL with cache functions (I believe we're up to R20 now, if I'm not mistaken).
The reason I created this was because I want to give people a small "hint" on how to use MySQL. You can either use the include as it is, or read it, to actually learn how to use MySQL with the original MySQL include.
I hope someone can put this to good use.
Functions
Usage Example
Note: The query size is defined in the top of the include. The default is left at 300 cells, if you want it to be bigger than that, you can change it.
Download
Download Here
Note: This can only be used with the newest MySQL plugin, which can be found Here
If you find bugs or find something that can be improved, please notify me by either a post here or a PM : )
And again, this is for learning purposes. It's not thorougly tested (Though it should work).
And in case anyone wondered, it uses the functions cache_save and cache_set_active to load the information through functions.
Changelog
Version 1.1
I've been on these forums for a while, and I keep seeing people struggling to use the MySQL plugin by G-Stylez/BlueG. MySQL is hard to use, if you don't know HOW to use it. That's why I decided to create a small include with functions for loading and saving playerdata. I've made it as simple as possible. I haven't tested it with several players yet, but it should work pretty good.
This include uses the latest version of MySQL with cache functions (I believe we're up to R20 now, if I'm not mistaken).
The reason I created this was because I want to give people a small "hint" on how to use MySQL. You can either use the include as it is, or read it, to actually learn how to use MySQL with the original MySQL include.
I hope someone can put this to good use.
Functions
Quote:
|
pawn Code:
new MySQLCon;
public OnGameModeInit()
{
DB_Connect("localhost", "root", "samp", "12345", MySQLCon); // Connect to the database. MySQLCon will be our connection handler
DB_SetAccountTable("playerdb", "Name"); // Set the table for players
}
public OnPlayerConnect(playerid)
{
DB_CheckPlayer(playerid, MySQLCon); // Check if player exists
return 1;
}
public OnPlayerRequestClass(playerid, classid)
{
if(AccountExists[playerid])
SendClientMessage(playerid, Color, "/login to login.");
else
SendClientMessage(playerid, Color, "/register to register.");
}
YCMD:login(playerid, params[], help)
{
if(help) SendClientMessage(playerid, Color, "Used to log in.")
else
{
new Password[30];
if(sscanf(params, "s", Password))
{
SendClientMessage(playerid, Color, "* Usage: /login [Password]");
return 1;
}
else
{
// Encrypt password before you pass it on to DB_GetPlayerData
DB_GetPlayerData(playerid, Password, MySQLCon);
LoadPlayer(playerid);
}
}
}
enum pdata
{
PlayerLevel,
Float:X,
SomeString[20]
}
new pData[MAX_PLAYERS][pdata];
stock LoadPlayer(playerid)
{
if(DB_LoadPlayerData(playerid, MySQLCon))
{
pData[playerid][PlayerLevel] = DB_GetInt("Level", MySQLCon);
pData[playerid][X] = DB_GetFloat("SpawnX", MySQLCon);
pData[playerid][SomeString] = DB_GetString("SomeString", MySQLCon);
DB_Finish(playerid, MySQLCon);
return 1;
}
else SendClientMessage(playerid, 000000, "Wrong Password");
return 0;
}
stock SavePlayer(playerid)
{
if(IsPlayerConnected(playerid))
{
new PlayerName[24];
GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
new Test[10] = "John", Testvar = 19;
DB_SaveData(MySQLCon,
"ssdds", // String x2, Integer x2, String
"Name", "Knappen", // `Name`='Knappen',
"Friend", Test, // `Friend`='John',
"Money", 1, // `Money`=1,
"PlayerLevel", Testvar, // `Playerlevel`=19
"Name", PlayerName); // WHERE `Name`='Eirik' or whatever my name is.
return 1;
}
return 0;
}
Download
Download Here
Note: This can only be used with the newest MySQL plugin, which can be found Here
If you find bugs or find something that can be improved, please notify me by either a post here or a PM : )
And again, this is for learning purposes. It's not thorougly tested (Though it should work).
And in case anyone wondered, it uses the functions cache_save and cache_set_active to load the information through functions.
Changelog
Version 1.1
- DB_SaveData Added. You can now save data to your database with one line.
- sizeof parameter was added to DB_GetString. This was done to save up a few cells. Default size is 24 if you don't write it. You can still use it without the sizeof.
- Security taken into measure. It escapes all strings that gets saved to the database. Also password and playername gets escaped when checking an account.
- First version of the include. Most of the things needed to load player data is in this version.