my problem is if i add new table in mysql, and then when i try to login to the game, my character freezes. Thanks!
You'll have to provide some code. Especially the code you added.
PHP код:
#include <a_samp>
#include <a_mysql>
#define MYSQL_HOSTNAME "127.0.0.1"
#define MYSQL_USERNAME "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "samp"
#define START_X 2492.0830
#define START_Y -1665.6895
#define START_Z 13.3438
#define START_A 93.1757
new mysql;
native WP_Hash(buffer[], len, const str[]);
enum
{
DIALOG_LOGIN,
DIALOG_REGISTER
};
enum AccountData
{
ID,
Name[MAX_PLAYER_NAME],
Password[129],
IP[16]
};
new Account[MAX_PLAYERS][AccountData];
enum PlayerData
{
ID,
Name[MAX_PLAYER_NAME],
Admin,
VIP,
Money,
Float:PosX,
Float:PosY,
Float:PosZ,
Float:PosA
};
new Player[MAX_PLAYERS][PlayerData];
main()
{
}
public OnGameModeInit()
{
mysql_log(LOG_ALL);
mysql = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_DATABASE, MYSQL_PASSWORD);
if(mysql_errno() != 0)
{
printf("[MySQL] The connection has failed.");
}
else
{
printf("[MySQL] The connection was successful.");
}
return true;
}
public OnGameModeExit()
{
return true;
}
public OnPlayerRequestClass(playerid, classid)
{
return true;
}
public OnPlayerConnect(playerid)
{
TogglePlayerSpectating(playerid, true);
new query[128], playername[MAX_PLAYER_NAME];
GetPlayerName(playerid, playername, sizeof(playername));
mysql_format(mysql, query, sizeof(query), "SELECT `Password`, `ID` FROM `account_data` WHERE `Name` = '%e' LIMIT 1", playername);
mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
return true;
}
public OnPlayerDisconnect(playerid, reason)
{
new query[128], Float:pos[4];
GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
GetPlayerFacingAngle(playerid, pos[3]);
mysql_format(mysql, query, sizeof(query), "UPDATE `player_data` SET `Admin` = %d, `VIP` = %d, `Money` = %d, `PosX` = %f, `PosY` = %f, `PosZ` = %f, `PosA` = %f WHERE `ID` = %d",
Player[playerid][Admin], Player[playerid][VIP], GetPlayerMoney(playerid), pos[0], pos[1], pos[2], pos[3], Player[playerid][ID]);
mysql_tquery(mysql, query, "", "");
return true;
}
public OnPlayerSpawn(playerid)
{
return true;
}
public OnPlayerDeath(playerid, killerid, reason)
{
return true;
}
public OnVehicleSpawn(vehicleid)
{
return true;
}
public OnVehicleDeath(vehicleid, killerid)
{
return true;
}
public OnPlayerText(playerid, text[])
{
return true;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
return false;
}
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
return true;
}
public OnPlayerExitVehicle(playerid, vehicleid)
{
return true;
}
public OnPlayerStateChange(playerid, newstate, oldstate)
{
return true;
}
public OnPlayerEnterCheckpoint(playerid)
{
return true;
}
public OnPlayerLeaveCheckpoint(playerid)
{
return true;
}
public OnPlayerEnterRaceCheckpoint(playerid)
{
return true;
}
public OnPlayerLeaveRaceCheckpoint(playerid)
{
return true;
}
public OnRconCommand(cmd[])
{
return true;
}
public OnPlayerRequestSpawn(playerid)
{
return true;
}
public OnObjectMoved(objectid)
{
return true;
}
public OnPlayerObjectMoved(playerid, objectid)
{
return true;
}
public OnPlayerPickUpPickup(playerid, pickupid)
{
return true;
}
public OnVehicleMod(playerid, vehicleid, componentid)
{
return true;
}
public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
return true;
}
public OnVehicleRespray(playerid, vehicleid, color1, color2)
{
return true;
}
public OnPlayerSelectedMenuRow(playerid, row)
{
return true;
}
public OnPlayerExitedMenu(playerid)
{
return true;
}
public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
return true;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
return true;
}
public OnRconLoginAttempt(ip[], password[], success)
{
return true;
}
public OnPlayerUpdate(playerid)
{
return true;
}
public OnPlayerStreamIn(playerid, forplayerid)
{
return true;
}
public OnPlayerStreamOut(playerid, forplayerid)
{
return true;
}
public OnVehicleStreamIn(vehicleid, forplayerid)
{
return true;
}
public OnVehicleStreamOut(vehicleid, forplayerid)
{
return true;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_LOGIN:
{
if(!response) Kick(playerid);
new hashpass[129], query[128], playername[MAX_PLAYER_NAME];
GetPlayerName(playerid, playername, sizeof(playername));
WP_Hash(hashpass, sizeof(hashpass), inputtext);
if(!strcmp(hashpass, Account[playerid][Password]))
{
mysql_format(mysql, query, sizeof(query), "SELECT * FROM `player_data` WHERE `Name` = '%e' LIMIT 1", playername);
mysql_tquery(mysql, query, "OnPlayerLoad", "i", playerid);
}
else
{
SendClientMessage(playerid, -1, "You have specified an incorrect password!");
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Welcome player!\nYour account has been found in our database. Please fill in your password:", "Login", "Quit");
}
}
case DIALOG_REGISTER:
{
if(!response) return Kick(playerid);
if(strlen(inputtext) < 5)
{
SendClientMessage(playerid, -1, "Your password must at least contain more than 4 characters.");
return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Welcome player!\nYour account has not been registered yet. Please fill in your desired password:", "Register", "Quit");
}
new query[128], query2[512], playername[MAX_PLAYER_NAME], playerip[16];
GetPlayerName(playerid, playername, sizeof(playername));
GetPlayerIp(playerid, playerip, sizeof(playerip));
WP_Hash(Account[playerid][Password], 129, inputtext);
mysql_format(mysql, query, sizeof(query), "INSERT INTO `account_data` (`Name`, `Password`, `IP`) VALUES ('%e', '%e', '%e')", playername, Account[playerid][Password], playerip);
mysql_format(mysql, query2, sizeof(query), "INSERT INTO `player_data` (`Name`, `Admin`, `VIP`, `Money`, `PosX`, `PosY`, `PosZ`, `PosA`) VALUES ('%e', 0, 0, 0, %f, %f, %f, %f)", playername, START_X, START_Y, START_Z, START_A);
mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
mysql_tquery(mysql, query2, "OnPlayerRegister", "i", playerid);
}
}
return true;
}
public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
return true;
}
forward OnAccountCheck(playerid);
public OnAccountCheck(playerid)
{
new rows, fields;
cache_get_data(rows, fields, mysql);
if(rows)
{
cache_get_field_content(0, "Password", Account[playerid][Password], mysql, 129);
Account[playerid][ID] = cache_get_field_content_int(0, "ID");
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Welcome player!\nYour account has been found in our database. Please fill in your password:", "Login", "Quit");
}
else
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Welcome player!\nYour account has not been registered yet. Please fill in your desired password:", "Register", "Quit");
}
return true;
}
forward OnPlayerLoad(playerid);
public OnPlayerLoad(playerid)
{
Player[playerid][Admin] = cache_get_field_content_int(0, "Admin");
Player[playerid][VIP] = cache_get_field_content_int(0, "VIP");
Player[playerid][Money] = cache_get_field_content_int(0, "Money");
Player[playerid][PosX] = cache_get_field_content_float(0, "PosX");
Player[playerid][PosY] = cache_get_field_content_float(0, "PosY");
Player[playerid][PosZ] = cache_get_field_content_float(0, "PosZ");
Player[playerid][PosA] = cache_get_field_content_float(0, "PosA");
TogglePlayerSpectating(playerid, false);
GivePlayerMoney(playerid, Player[playerid][Money]);
SetSpawnInfo(playerid, 0, 23, Player[playerid][PosX], Player[playerid][PosY], Player[playerid][PosZ], Player[playerid][PosA], 0, 0, 0, 0, 0, 0);
SpawnPlayer(playerid);
SendClientMessage(playerid, -1, "You have successfully logged in.");
return true;
}
forward OnAccountRegister(playerid);
public OnAccountRegister(playerid)
{
Account[playerid][ID] = cache_insert_id();
printf("[Registration] New account registered. Database ID: [%d]", Account[playerid][ID]);
TogglePlayerSpectating(playerid, false);
SetSpawnInfo(playerid, 0, 23, START_X, START_Y, START_Z, START_A, 0, 0, 0, 0, 0, 0);
SpawnPlayer(playerid);
return true;
}
forward OnPlayerRegister(playerid);
public OnPlayerRegister(playerid)
{
Player[playerid][ID] = cache_insert_id();
printf("[Registration] New account registered. Database ID: [%d]", Player[playerid][ID]);
TogglePlayerSpectating(playerid, false);
SetSpawnInfo(playerid, 0, 23, START_X, START_Y, START_Z, START_A, 0, 0, 0, 0, 0, 0);
SpawnPlayer(playerid);
return true;
}
You're adding a row with data to two different tables (tables which have the same purpose?) and you call two different functions (which have the same purpose!) at the same time. Merge the queries and adjust your database accordingly.
I want to separate it, may you can help me improve this? If the account is created it will automatically generate id and name on a no data database.
Best is to add default values to columns that have a non-constant value upon creating a row. Columns such as: Admin, VIP, Money, PosX to PosZ all have constant values when you create the player's row.