21.04.2018, 16:07
I'm trying to learn how to store server data with mysql and I have two problems.
The first one is that when I use cache_get_row_count it always returns 0 even though there's a matching result in the database.
The second problem is that when I use cache_get_name_value I can't seem to store it in an array.
Problematic code:
Full code:
And as a sidenote, can anyone recommend some good blueg mysql r41 tutrials? Because the one tutorial on the forum doesn't explain the most important parts and the documentation on samp wiki is lacking at best.
The first one is that when I use cache_get_row_count it always returns 0 even though there's a matching result in the database.
The second problem is that when I use cache_get_name_value I can't seem to store it in an array.
Problematic code:
Код:
public OnPlayerConnect(playerid) { new query[128]; GetPlayerName(playerid, pInfo[playerid][pUsername], MAX_PLAYER_NAME); mysql_format(database, query, sizeof query, "SELECT * FROM `users` WHERE `username` = %e LIMIT 1", pInfo[playerid][pUsername]); mysql_tquery(database, query); pInfo[playerid][playerCache] = cache_save(); cache_set_active(pInfo[playerid][playerCache]); new rowCount; cache_get_row_count(rowCount); // -- HERE IS THE PROBLEM I ASSUME if(rowCount > 0) { new test[256]; // don't mind this it's for debugging cache_get_value_name(0, "password", pInfo[playerid][pPass], 65); // ---- HERE IS THE PROBLEM I ASSUME format(test, sizeof test, "password: %s", pInfo[playerid][pPass]); // don't mind this it's for debugging SendClientMessage(playerid, -1, test); // don't mind this it's for debugging ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit"); } else { ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Type a password to register.", "Register", "Exit"); } return 1; }
Код:
#include <a_samp> #include <a_mysql> #define DIALOG_LOGIN 0 #define DIALOG_REGISTER 1 #define ms_host "localhost" #define ms_user "myname" #define ms_password "mypassword" #define ms_database "mydbname" new MySQL:database; enum playerData { pID, pUsername[25], pPass[65], Cache:playerCache } new pInfo[MAX_PLAYERS][playerData]; public OnGameModeInit() { database = mysql_connect(ms_host, ms_user, ms_password, ms_database); mysql_tquery(database, "CREATE TABLE IF NOT EXISTS `users` (`id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(24) NOT NULL, `password` char(65) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `USERNAME` (`USERNAME`))"); return 1; } public OnGameModeExit() { for(new i=0; i<MAX_PLAYERS;i++) { if(IsPlayerConnected(i)) { Kick(i); } } return 1; } public OnPlayerConnect(playerid) { new query[128]; GetPlayerName(playerid, pInfo[playerid][pUsername], MAX_PLAYER_NAME); mysql_format(database, query, sizeof query, "SELECT * FROM `users` WHERE `username` = %e LIMIT 1", pInfo[playerid][pUsername]); mysql_tquery(database, query); pInfo[playerid][playerCache] = cache_save(); cache_set_active(pInfo[playerid][playerCache]); new rowCount; cache_get_row_count(rowCount); if(rowCount > 0) { new test[256]; cache_get_value_name(0, "password", pInfo[playerid][pPass], 65); format(test, sizeof test, "password: %s", pInfo[playerid][pPass]); SendClientMessage(playerid, -1, test); ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit"); } else { ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Type a password to register.", "Register", "Exit"); } return 1; } public OnPlayerDisconnect(playerid, reason) { return 1; } public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { if(dialogid == DIALOG_LOGIN) { if(!response) return Kick(playerid); new hashedPassword[65]; SHA256_PassHash(inputtext, "78sdjs86d2h", hashedPassword, sizeof hashedPassword); if(strcmp(hashedPassword, pInfo[playerid][pPass]) == 0) { SendClientMessage(playerid, -1, "Welcome back!"); } else { SendClientMessage(playerid, -1, "Wrong password, try again."); ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit"); } } else if(dialogid == DIALOG_REGISTER) { if(!response) return Kick(playerid); new query[256]; new test[256]; SHA256_PassHash(inputtext, "78sdjs86d2h", pInfo[playerid][pPass], 65); format(test, sizeof test, "name: %s, pass: %s", pInfo[playerid][pUsername], pInfo[playerid][pPass]); SendClientMessage(playerid, -1, test); mysql_format(database, query, sizeof query, "INSERT INTO `users` (`username`, `password`) VALUES ('%e', '%s')", pInfo[playerid][pUsername], pInfo[playerid][pPass]); mysql_tquery(database, query); ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit"); } return 1; } public OnPlayerRequestSpawn(playerid) { SetSpawnInfo(playerid, 0, 240,1484.4651,-1742.1217,13.5469,2.9337,0,0,0,0,0,0); SpawnPlayer(playerid); return 1; }