[MySQL] Loading a variable.
#1

Hey all,

This is what I am doing to store a variable:
pawn Код:
PlayerInfo[playerid][pSkillSelected] = 1;
mysql_format(mysql, f_query, sizeof(f_query), "UPDATE playerdata SET SkillSelected='%i' WHERE name='%s'", PlayerInfo[playerid][pSkillSelected], PlayerInfo[playerid][name]);
mysql_tquery(mysql, f_query, "", "");
And now I want to load this variable:
pawn Код:
forward LoadAccount(playerid);
public LoadAccount(playerid)
{
    new rows = cache_num_rows();
    if(!rows) {
        printf("[DATABASE ERROR]: Account does not exist.");
    }
    else if(rows == 1) {
        cache_get_field_content(0, "IP", PlayerInfo[playerid][IP], mysql, 17);
        cache_get_field_content(0, "Sex", PlayerInfo[playerid][pSex], mysql);
        cache_get_field_content(0, "Age", PlayerInfo[playerid][pAge], mysql);
        cache_get_field_content(0, "Medic", PlayerInfo[playerid][pAthelete], mysql);
        cache_get_field_content(0, "Mechanic", PlayerInfo[playerid][pMechanic], mysql);
        cache_get_field_content(0, "Pilot", PlayerInfo[playerid][pPilot], mysql);
        cache_get_field_content(0, "SkillSelected", PlayerInfo[playerid][pSkillSelected]);
    }
    else if(rows > 1) {
        printf("[DATABASE ERROR]: Multiple accounts matching player %d.", playerid);
    }
}
And I want to use that result here:
pawn Код:
if(PlayerInfo[playerid][pSkillSelected] != 1)
{
        ShowPlayerDialog(playerid, DIALOG_SKILL, DIALOG_STYLE_LIST, "Select the skill you want your chracter to have","Medic\nAthlete\nPilot\nMechanic","Select","Quit");
        print("yeah!");
}
But it is showing me the dialog and printing 'yeah'.

Am I doing it right?
Reply
#2

Anyways you're wrong here, all will load the first row in the database, even though you could use cache_get_field_content_int, cache_get_field_content_float if its int or float but it doesn't matter afaik.

pawn Код:
cache_get_field_content(0, "IP", PlayerInfo[playerid][IP], mysql, 17);
cache_get_field_content(1, "Sex", PlayerInfo[playerid][pSex], mysql);
cache_get_field_content(2, "Age", PlayerInfo[playerid][pAge], mysql);
cache_get_field_content(3, "Medic", PlayerInfo[playerid][pAthelete], mysql);
cache_get_field_content(4, "Mechanic", PlayerInfo[playerid][pMechanic], mysql);
cache_get_field_content(5, "Pilot", PlayerInfo[playerid][pPilot], mysql);
cache_get_field_content(6, "SkillSelected", PlayerInfo[playerid][pSkillSelected], mysql);
EDIT: You could also use %e specifier to escape a string automatically.
Reply
#3

Dayum! Almost forgot to alter the first parameter. Copy paste fail. Before I tried cache_get_filed_content_int but changed to just cache_get_field_content. So as you are saying it won't affect the code, right?

Note: New to MySQL.

EDIT: Your DP, xD

EDIT2: Yep I used it at other places.
Reply
#4

Quote:
Originally Posted by Faisal_khan
Посмотреть сообщение
Dayum! Almost forgot to alter the first parameter. Copy paste fail. Before I tried cache_get_filed_content_int but changed to just cache_get_field_content. So as you are saying it won't affect the code, right?

Note: New to MySQL.

EDIT: Your DP, xD

EDIT2: Yep I used it at other places.
Oh Wait, if you're using cache_get_field_content you need to convert string into an integer but I'd suggest you use cache_get_field_int and cache_get_field_float as its doing the job for you.
Reply
#5

Well cache_get_field_content_int also did not worked. Same problem persists.
Reply
#6

Try using cache_get_field_content_int
pawn Код:
PlayerInfo[playerid][pSkillSelected] = cache_get_field_content_int(0, "SkillSelected");
Reply
#7

Quote:
Originally Posted by Faisal_khan
Посмотреть сообщение
Well cache_get_field_content_int also did not worked. Same problem persists.
Mind reading this. Any other solution?
Reply
#8

Turn the mysql debug on, also whats the query that loads data from database and forwards to LoadAccount(playerid)?
it should be like this for ex
pawn Код:
new string[70];
                format(string, sizeof(string),"SELECT * FROM `Users` WHERE `Name` = '%s'",The string containing the name here);
                mysql_function_query(1, string,true,"LoadAccount","d",playerid);
Are you sure the connection is alive?
Reply
#9

pawn Код:
SendClientMessage(playerid, COLOR_YELLOW, "You have succesfully logged in!");
                    IsLogged[playerid] = 1;
                    mysql_format(mysql, query, sizeof(query), "SELECT * FROM playerdata WHERE Name = '%e' LIMIT 1", PlayerInfo[playerid][name]);
                    mysql_tquery(mysql, query, "LoadAccount", "d", playerid);
                    if(GenderSelected[playerid] != 1) ShowPlayerDialog(playerid, DIALOG_GENDER, DIALOG_STYLE_MSGBOX, "Gender","Please select the gender of your character","Male","Female");
                    if(PlayerInfo[playerid][pSkillSelected] != 1)
                    {
                        ShowPlayerDialog(playerid, DIALOG_SKILL, DIALOG_STYLE_LIST, "Select the skill you want your chracter to have","Medic\nAthlete\nPilot\nMechanic","Select","Quit");
                        print("yeah!");
                    }
Yeah the connection is alive.
Reply
#10

mysql_debug was for older version, I am using mysql_log. And it iis giving me these errors. Will someone explain me how to use the row index parameter?

Debug errors:
Код:
In callback "LoadAccount"
Time	Function	Status	Message
14:41:20	CMySQLResult::GetRowDataByName()	ERROR	invalid row index ('1')
14:41:20	CMySQLResult::GetRowDataByName()	ERROR	invalid row index ('2')
14:41:20	CMySQLResult::GetRowDataByName()	ERROR	invalid row index ('3')
14:41:20	CMySQLResult::GetRowDataByName()	ERROR	invalid row index ('4')
14:41:20	CMySQLResult::GetRowDataByName()	ERROR	invalid row index ('5')
14:41:20	CMySQLResult::GetRowDataByName()	ERROR	invalid row index ('6')
EDIT:
I changed this:
pawn Код:
cache_get_field_content(0, "IP", PlayerInfo[playerid][IP], mysql);
        cache_get_field_content(1, "Sex", PlayerInfo[playerid][pSex], mysql);
        cache_get_field_content(2, "Age", PlayerInfo[playerid][pAge], mysql);
        cache_get_field_content(3, "Medic", PlayerInfo[playerid][pAthelete], mysql);
        cache_get_field_content(4, "Mechanic", PlayerInfo[playerid][pMechanic], mysql);
        cache_get_field_content(5, "Pilot", PlayerInfo[playerid][pPilot], mysql);
        cache_get_field_content(6, "SkillSelected", PlayerInfo[playerid][pSkillSelected], mysql);
now no error for invalid datatype.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)