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
public OnFilterScriptInit()
{
yoursql_connect("localhost","root","users","");
return 1;
}
//this will connect to the mysql server
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;
}
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.
*/
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
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: '|')
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.
You should also contemplate creating a function that can parse multiple values
|
yoursql_insert(blababla, "isf", 1, string, float);
Something like:
pawn Код:
|
You should also contemplate creating a function that can parse multiple values and query threading.
|