SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 07/05/2012, 04:23 PM   #1
C0dy09
Little Clucker
 
C0dy09's Avatar
 
Join Date: Oct 2011
Location: Germany
Posts: 36
Reputation: 0
Default Threaded Queries

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:

Code:
#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:

Code:
#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 ^^

Last edited by C0dy09; 07/05/2012 at 04:32 PM. Reason: corriger code
C0dy09 is offline   Reply With Quote
Old 07/05/2012, 09:57 PM   #2
JaTochNietDan
High-roller
 
JaTochNietDan's Avatar
 
Join Date: Apr 2007
Location: Republic of Ireland
Posts: 2,502
Reputation: 363
Default Re: Threaded Queries

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.
__________________
Releases:
[Includes]: [jBan] | [jBanLite]
[Plugins]: [FileManager] | [Execute]
[Applications]: [Remote Administrator *NEW] | [Script King] | [RCON GUI]
JaTochNietDan is offline   Reply With Quote
Old 07/05/2012, 10:09 PM   #3
Scenario
Godfather
 
Scenario's Avatar
 
Join Date: Feb 2010
Location: Heaven
Posts: 7,801
Reputation: 701
Default Re: Threaded Queries

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.
Scenario is offline   Reply With Quote
Old 08/05/2012, 01:41 PM   #4
C0dy09
Little Clucker
 
C0dy09's Avatar
 
Join Date: Oct 2011
Location: Germany
Posts: 36
Reputation: 0
Default AW: Threaded Queries

Thank´s

so if i understand i should use something like that:

Code:
 
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)"
C0dy09 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Question] Threaded scripts? tony_fitto Scripting Help 5 27/07/2018 02:40 PM
Threaded timers??? Programie Scripting Help 2 23/04/2012 10:26 PM
[Tutorial] Threaded query AppleX Lançamentos/Releases 5 23/01/2012 07:39 PM
SendClientMessage fails to work in some threaded queries. Why? _Outbreak_ Scripting Help 3 30/09/2011 11:44 AM
Threaded queries armyoftwo Help Archive 0 11/12/2010 01:49 PM


All times are GMT. The time now is 09:52 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.