13.03.2011, 03:10
(
Последний раз редактировалось gamer931215; 09.05.2011 в 21:47.
)
yourSQL 1.3
About
YourSQL is a very simple include that simplifies the usage of MYSQL querys in pawn.
However, you must have already a database structure, if you dont have it ? you can set one up with PhpmyAdmin.
It has the following functions:
Код:
yoursql_connect(host[],username[],database[],password[])//returns nothing yoursql_close()//closes the connection (in case you want to switch to another database, this is standard called when you exit your gamemode/filterscript) yoursql_insert(table[],type[],{float,_}:...)UPDATED!//returns 1 if the query was executed, 0 when there was an mysql error yoursql_update(table[],updater[],identifier[])//returns 1 if the query was executed, 0 when there was an mysql error yoursql_delete(table[],identifier[])//returns 1 if the query was executed, 0 when there was an mysql error yoursql_receive(strquery[])//will return the splitted information if the query was executed, and the query itself if there was a mysql_error yoursql_countrows(strquery[])//returns the ammount of selected rows yoursql_escape_string(str[]);//Returns escaped string
yoursql_connect(host,username,database,password)
This will connect to the database, there is no need to close the connection (this will automaticly be done on GameModeExit or OnFilterScriptExit).
Example script:
pawn Код:
public OnFilterScriptInit()
{
yoursql_connect("localhost","root","users","");
return 1;
}
//this will connect to the mysql server
yoursql_insert(table[],type[],{Float,_}:...) & yoursql_escape_string(str)
This allows you to insert easily data into your database.
Example script:
pawn Код:
stock OnPlayerRegister(playerid)
{
yoursql_insert("users","sii",yoursql_escape_string(PlayerInfo[playerid][UserName]),PlayerInfo[playerid][money],PlayerInfo[playerid][score]);
/*
This will insert all the info from the enum in the database
Explaining the input:
table: users
types: string(s), int(i) and another int(i)
values: PlayerInfo[playerid][UserName],PlayerInfo[playerid][money],PlayerInfo[playerid][score]
Available types:
s: string
i: int
f: float
About the values:
Please note that you ALWAYS need to use yoursql_escape_string() before entering/updating a string comming directly from the user. Else your database will be voulnerable to mysql injections!
*/
return 1;
}
This will allow you to update existing data in your database.
Example script:
pawn Код:
stock SavePlayer(playerid)
{
new string[128],string2[64],pName[MAX_PLAYER_NAME];
GetPlayerName(playerid,pName,sizeof pName);
format(string,sizeof string,"'%s',%i,%i",yoursql_escape_string(PlayerInfo[playerid][UserName]),PlayerInfo[playerid][money],PlayerInfo[playerid][score]);
format(string2,sizeof string2,"Username = '%s'",pName); //the identifier, so it will actually execute UPDATE blablabla WHERE Username = (player's username here)
yoursql_update("users",string,string2)
return 1;
}
/*this will update all the changes from the enum in the database.
Table: Users
New values: yoursql_escape_string(PlayerInfo[playerid][UserName]),PlayerInfo[playerid][money],PlayerInfo[playerid][score]
*!Please notice that this works just like a query, if you update a string you NEED to use quotemarks for them (instead of value you use 'value')
Identifier: Where username = yourname //so it will only update where the username is
equal to the selected rows, you need to have a little bit knowledge about mysql using this.
*/
This allows you to delete a record from the database.
Example script:
pawn Код:
COMMAND:deleteme(playerid,params[])
{
new string[64],pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName);
format(string,sizeof string,"WHERE username = '%s'",pName);
yoursql_delete("users",string);
return 1;
}
//this will remove the player from the database
This will read data from the database
Example script:
pawn Код:
stock OnPlayerLogin(playerid)
{
new string[128],pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
format(string,sizeof string,"SELECT * FROM USERS WHERE `username` = '%s'",pName);
sscanf(yoursql_receive(string),"p<|>e<s[MAX_PLAYER_NAME]ii>",PlayerInfo[playerid]); //the function returns value1|value2|value3, with sscanf you can split this, or put it directly in a enum!
return 1;
}
//receives the data from the database, it will receive all data in one string!
You can use sscanf to split them (split character: '|')
This will count the selected rows in a query
Example script:
pawn Код:
stock IsPlayerAlreadyRegistered(playerid)
{
new string[128],pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
format(string,sizeof string,"SELECT * FROM users WHERE username = 's'",pName);
if(yoursql_countrows(strquery) > 0) return true;
return false;
}
//counts selected rows, in this case:
if now row has been selected (so there ISNT a row where username = (player's name)), the player is not registered.
I specially want to thank ******, Ryder & Hiddos for helping me figuring out the getarg/numarg part. This was really a pain in the arse finding out that my windows XP was bugged (so it never worked at that pc, and DID work on my other computers) and this was not possible without them.
DOWNLOAD:
The include:
http://pastebin.com/VTyARFn1
G-sTyLeZzZ's MYSQL plugin:
https://sampforum.blast.hk/showthread.php?tid=56564