22.04.2011, 15:35
Okay, I don`t have much time and I hope you`re a fast learner. I`ll show you my Loading and Saving function for MySQL and a brief explanation and I hope you`ll understand.
Loading:
So, the idea is: You connect to the server, you format the query string, you then send the query, store the result, fetch the result into an array(!) and then use the sscanf to get your data from the string.
Saving:
Here the idea is: You connect to the server, format the query string and then query the string. No further need to store any results since you don`t 'download' anything, you just 'upload'.
As a reminder in the end, when you interact with user input/name/etc always use mysql_real_escape_string(string, escape_string);
Hope you understood. If not, reply! Good luck!
Loading:
Код:
public LoadVariables(playerid) { //connection mysql_debug(1); mysql_connect(HOST, USER, DATABASE, PASSWORD); //query new EscapeName[MAX_PLAYER_NAME], Name[MAX_PLAYER_NAME]; GetPlayerName(playerid, Name, sizeof(Name)); mysql_real_escape_string(Name, EscapeName); new Query[256]; format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s'", EscapeName); mysql_query(Query); mysql_store_result(); new Result[128]; mysql_fetch_row_format(Result, " "); //variables selection new username[50], p_password[30], level, alevel, money, xp, Float:lastX, Float:lastY, Float:lastZ, Float:lastRot, loginSpawn; sscanf(Result, "s[50]s[30]iiiiffffi", username, p_password, level, alevel, money, xp, lastX, lastY, lastZ, lastRot, loginSpawn); SetPVarString(playerid, "username", username); SetPVarString(playerid, "password", p_password); SetPVarInt(playerid, "level", level); SetPVarInt(playerid, "alevel", alevel); SetPVarInt(playerid, "money", money); SetPVarInt(playerid, "xp", xp); SetPVarFloat(playerid, "lastX", lastX); SetPVarFloat(playerid, "lastY", lastY); SetPVarFloat(playerid, "lastZ", lastZ); SetPVarFloat(playerid, "lastRot", lastRot); SetPVarInt(playerid, "loginSpawn", loginSpawn); SetPVarInt(playerid, "IsSpawnedFromIntro", 1); SetPVarInt(playerid, "DiscFromGMX", 0); //connection close mysql_close(); return 1; }
Saving:
Код:
: public SaveVariables(playerid) { //connection mysql_debug(1); mysql_connect(HOST, USER, DATABASE, PASSWORD); //query new EscapeName[MAX_PLAYER_NAME], Name[MAX_PLAYER_NAME]; GetPlayerName(playerid, Name, sizeof(Name)); mysql_real_escape_string(Name, EscapeName); new Float:lastX, Float:lastY, Float:lastZ, Float:lastRot; GetPlayerPos(playerid, lastX, lastY, lastZ); GetPlayerFacingAngle(playerid, lastRot); new Query[256]; format(Query, sizeof(Query), "UPDATE users SET level = '%i', alevel = '%i', money = '%i', xp = '%i', lastX = '%f', lastY = '%f', lastZ = '%f', lastRot = '%f' WHERE username = '%s'", GetPVarInt(playerid, "level"), GetPVarInt(playerid, "alevel"), GetPVarInt(playerid, "money"), GetPVarInt(playerid, "xp"), lastX, lastY, lastZ, lastRot, EscapeName); mysql_query(Query); //connection close mysql_close(); return 1; }
As a reminder in the end, when you interact with user input/name/etc always use mysql_real_escape_string(string, escape_string);
Hope you understood. If not, reply! Good luck!