Threaded Queries -
C0dy09 - 07.05.2012
Hello all,
i know about threaded queries, but i have some questions:
-for example a login process:
How I have to write the Script and OnQueryFinish that it prevent from server lagging?
this way:
Код:
#define MYSQL_CHECK_PLAYER (1)
stock mysql_CheckAccount(playerid)
{
new Name[MAX_PLAYER_NAME];
GetPlayerName(playerid, Name, MAX_PLAYER_NAME);
mysql_real_escape_string(Name, Name);
format(query, sizeof(query), "SELECT * FROM "MYSQL_ACC_TBL" WHERE Name = '%s' ORDER BY `id` DESC LIMIT 1", Name);
mysql_query(query,MYSQL_CHECK_PLAYER,playerid);
mysql_store_result();
return mysql_num_rows();
}
public OnPlayerConnect(playerid)
{
if(mysql_CheckAccount(playerid) == 1)
{
gPlayerAccount[playerid] = 1;
return 1;
}
else
{
gPlayerAccount[playerid] = 0;
return 1;
}
return 1;
}
public OnQueryFinish(query[], resultid, extraid, connectionHandle)
{
switch(resultid)
{
case MYSQL_CHECK_PLAYER:
{
return 1;
}
}
return 1;
}
or maybe this way:
Код:
#define MYSQL_CHECK_PLAYER (1)
stock mysql_CheckAccount(playerid)
{
new Name[MAX_PLAYER_NAME];
GetPlayerName(playerid, Name, MAX_PLAYER_NAME);
mysql_real_escape_string(Name, Name);
format(query, sizeof(query), "SELECT * FROM "MYSQL_ACC_TBL" WHERE Name = '%s' ORDER BY `id` DESC LIMIT 1", Name);
mysql_query(query,MYSQL_CHECK_PLAYER,playerid);
}
public OnPlayerConnect(playerid)
{
mysql_CheckAccount(playerid);
return 1;
}
public OnQueryFinish(query[], resultid, extraid, connectionHandle)
{
switch(resultid)
{
case MYSQL_CHECK_PLAYER:
{
mysql_store_result();
if(mysql_num_rows() ==1) gPlayerAccount[extraid] = 1;
else gPlayerAccount[extraid] = 0;
}
}
return 1;
}
i hope you can help me understand this better/corriger my code
MfG. Cody09
//IÒ‘m going to learn english

^^
Re: Threaded Queries -
JaTochNietDan - 07.05.2012
The latter example you provided is absolutely correct.
One optimization you should consider is your SELECT statement, are you actually going to do anything with all of the results you got (* will make it return all of the columns from any found rows)? If you're not using any of the information from the columns that you returned, it doesn't seem to make sense to make it go through the hassle of finding the information and returning it in the first place, which can increase the time it takes to execute a query dramatically depending on how many columns you have.
So consider only selecting a single column if you are not going to use any columns to speed up the query process.
Re: Threaded Queries -
Scenario - 07.05.2012
The latter one, as JaTochNietDan pointed out, would work fine.
It's good to see more people taking an interest in using threaded SQL queries.
AW: Threaded Queries -
C0dy09 - 08.05.2012
ThankÒ‘s
so if i understand i should use something like that:
Код:
format(query, sizeof(query), "SELECT COUNT(*) FROM "MYSQL_ACC_TBL" WHERE Name = '%s' ORDER BY `id` DESC LIMIT 1", Name);
mysql_query(query,MYSQL_CHECK_PLAYER,playerid);
is the best solution?
Another question:
-How to use the "COUNT(*)" function?
or what does it return exactly?
and what returns f.e.
"COUNT(id)" or "COUNT(passwort)"