PDA

View Full Version : Q: MySQL


Uberanwar
25/06/2018, 08:58 PM
Hi, I got a question regarding MySQL, I am pretty new on this.

How can I check if a field is NULL? And how can I read an integer value from a field correctly?

I have something like this in my script


PlayerData[extraid][pExam] = cache_get_field_int(0, "Exam");

if(!PlayerData[playerid][pExam]) StartExam(playerid);


Although the pExam field value is 1, it still starts the exam.

Calisthenics
25/06/2018, 09:32 PM
In old version, there was macro called ismysqlnull. In R40 and above, there is function cache_is_value_null

Uberanwar
25/06/2018, 11:25 PM
Alright, thank you will look into that.

How about the Exam field issue?

div
26/06/2018, 03:05 AM
cache get field content in a new defined variable, then compare the value


PlayerData[extraid][pExam] = cache_get_field_int(0, "Exam");

if(!PlayerData[playerid][pExam]) >=1)

//REST CODE GOES HERE
StartExam(playerid);



So, it will only allow 1 or more than 1, not less than that.

Sew_Sumi
26/06/2018, 05:18 AM
cache get field content in a new defined variable, then compare the value

if(!PlayerData[playerid][pExam]) >=1)

So, it will only allow 1 or more than 1, not less than that.

That's broken... You've got another bracket in there.

If you check a variable with the ! operator, it's a not, so anything other than 1, being 0, or -1 will return a result.

The if statement the OP had, should at least give some result.



OP, chuck a print statement in there so you know whether it's actually hitting that check and whether it's making it out the other side.

Calisthenics
26/06/2018, 07:04 AM
How about the Exam field issue?

Your original code:

PlayerData[extraid][pExam] = cache_get_field_int(0, "Exam");

if(!PlayerData[playerid][pExam]) StartExam(playerid);

GTLS
26/06/2018, 08:19 AM
Print the value for pExam for debug processes.

Tip: Use R40+.

Uberanwar
06/07/2018, 06:58 AM
This is what I do


public OnPlayerRequestClass(playerid, classid)
{
if(IsPlayerNPC(playerid)) return 1;

if (!PlayerData[playerid][pAccount] && !PlayerData[playerid][pKicked])
{
/*new
time[3];

gettime(time[0], time[1], time[2]);
SetPlayerTime(playerid, time[0], time[1]);*/

PlayerData[playerid][pAccount] = 1;
TogglePlayerSpectating(playerid, 1);

SetPlayerColor(playerid, DEFAULT_COLOR);
SetTimerEx("AccountCheck", 400, false, "d", playerid); // 400 ms
}
return true;
}



forward AccountCheck(playerid);
public AccountCheck(playerid)
{
//SetPlayerPos(playerid, -1988.752075, -72.294998, 38.647026);
//SetPlayerCameraPos(playerid, -1988.752075, -72.294998, 58.647026);
//SetPlayerCameraLookAt(playerid, -2006.489868, -72.107597, 55.977474);

SetCameraData(playerid);
SQL_CheckAccount(playerid);
return true;
}



SQL_CheckAccount(playerid)
{
new
query[128];

format(query, sizeof(query), "SELECT `Username` FROM `characters` WHERE `Character` = '%s'", ReturnName(playerid));
mysql_tquery(g_iHandle, query, "OnQueryFinished", "dd", playerid, THREAD_FIND_USERNAME);
}



OnQueryFinished
..
case THREAD_FIND_USERNAME:
{
static
query[1000]; // previous: 128

cache_get_row_count(rows);
cache_get_field_count(fields);

if (rows)
{
new
name[MAX_PLAYER_NAME + 1];

cache_get_value_index(0, 0, name);

if (strcmp(name, PlayerData[extraid][pUsername], false) != 0)
{
format(PlayerData[extraid][pUsername], sizeof(name), name);
SetPlayerName(extraid, name);
}
}
format(query, sizeof(query), "SELECT * FROM `accounts` WHERE `Username` = '%s'", PlayerData[extraid][pUsername]);
mysql_tquery(g_iHandle, query, "OnQueryFinished", "dd", extraid, THREAD_CHECK_ACCOUNT);
}



case THREAD_CHECK_ACCOUNT:
{
//cache_get_data(rows, fields, g_iHandle);
cache_get_row_count(rows);
cache_get_field_count(fields);

if (rows)
{
static
loginDate[36];

cache_get_value_index(0, 0, loginDate);

format(PlayerData[extraid][pLoginDate], 36, loginDate);

PlayerData[extraid][pExam] = cache_get_field_int(0, "Exam");

cache_get_value_name(0, "SecurityQuestion1", PlayerData[extraid][pSecurityQuestion1], 128);
cache_get_value_name(0, "SecurityAnswer1", PlayerData[extraid][pSecurityAnswer1], 65);

cache_get_value_name(0, "SecurityQuestion2", PlayerData[extraid][pSecurityQuestion2], 128);
cache_get_value_name(0, "SecurityAnswer2", PlayerData[extraid][pSecurityAnswer2], 65);

cache_get_value_name(0, "SecurityQuestion3", PlayerData[extraid][pSecurityQuestion3], 128);
cache_get_value_name(0, "SecurityAnswer3", PlayerData[extraid][pSecurityAnswer3], 65);

Dialog_Show(extraid, LoginScreen, DIALOG_STYLE_PASSWORD, "Account Login", "Welcome back to Asauth RPG!\n\nYour account was last seen on: %s.\n\nPlease enter your password below to login to your account:", "Login", "Cancel", PlayerData[extraid][pLoginDate]);
}
else
{
Dialog_Show(extraid, RegisterScreen, DIALOG_STYLE_PASSWORD, "Account Registration", "Welcome to Asauth RPG, %s.\n\nNotice: Your account is not registered yet. Please enter your desired password:", "Register", "Cancel", ReturnName(extraid));
}
}


In the logs it says no active cache, which could be the factor why the exam system keeps detecting that the player pExam value is 0..

Calisthenics
06/07/2018, 07:10 AM
Does it report any other error/warning in mysql logs? If you compile with debug info (https://github.com/Zeex/samp-plugin-crashdetect/wiki/Compiling-scripts-with-debug-info), it will report exact line to make it easier for both of us.

Uberanwar
06/07/2018, 07:29 AM
Does it report any other error/warning in mysql logs? If you compile with debug info (https://github.com/Zeex/samp-plugin-crashdetect/wiki/Compiling-scripts-with-debug-info), it will report exact line to make it easier for both of us.


No, only the 'cache_get_value_name: no active cache'.

I did compile with -d3.

Calisthenics
06/07/2018, 07:32 AM
Show line 7262 in AsauthRoleplay.pwn

Uberanwar
06/07/2018, 07:38 AM
Show line 7262 in AsauthRoleplay.pwn



...

cache_get_field_int(row, const field_name[])
{
new
str[12];

cache_get_value_name(row, field_name, str, sizeof(str)); // 7262
return strval(str);
}

...

Calisthenics
06/07/2018, 07:43 AM
I didn't notice until now that is a custom function. Why not just?

cache_get_value_int(0, "Exam", PlayerData[extraid][pExam]);

Anyway, then it is the next line reported (line 25070 from AsauthRoleplay.pwn).

Uberanwar
06/07/2018, 08:19 AM
I fixed the no active cache issue. I didn't realize there was an unused cache_get_value_ function inside OnPlayerSpawn and I've removed it now.

Right now, I am having an issue whereby the pExam value not being set properly.


new query[150];

format(query, sizeof(query), "UPDATE `accounts` SET `Exam` = '1' WHERE `ID` = '%d", PlayerData[playerid][ID]);
mysql_tquery(g_iHandle, query);


Once I have finished the exam, it should set the pExam value to 1 in the database. However it's not setting it to 1. What seems to be the problem here? Am I doing something wrong in the code above?

Calisthenics
06/07/2018, 08:26 AM
In the code above, there is a missing apostrophe at the end:

UPDATE `accounts` SET `Exam` = '1' WHERE `ID` = '%d

Have you tried to execute the query and not received any error in syntax?