Raised This Month: $ Target: $400
 0% 

SQLx to global variable problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 01-07-2007 , 01:09   SQLx to global variable problem
Reply With Quote #1

I have a problem with calling SQL queries and storing data in global variables. I have a test function that is called when a client types in a command in his console. I wanted to be able to store this player's data in a global array for his specific player id in the array. (Array[id])

The problem comes in when I call the SQL_Threadquery, which in turn uses a query handler with set parameters. I have no idea I'm supposed to pass a player's id into the handler function, or if it is even possible, so I decided to use a global variable to store the retrieved information to carry back into the function that called the Threadquery in the first place, which has an instance of the player's playerid.

I noticed that no data was returning when I printed it to the screen for the player to see, so I did a test, and printed out the result in two different places: one local, one global. The local print is the correct data, but the global print fails miserably.

Here are the functions...

PHP Code:
public SQL_CallQuery(Query[],Type)
{
 
copy(SQL_CurrentQuery,511,Query)
 
 
SQL_CurrentType=Type
 
 SQL_ThreadQuery
(SQL_Tuple,"SQL_QueryHandle",Query)

PHP Code:
public SQLTEST(id)
{
 new 
Query[256]
 
 
format(Query,255,"SELECT JobID FROM playerdata WHERE SteamID='%s'",UserSteamID[id])
 
 
SQL_ResultInt=1337
 
 SQL_CallQuery
(Query,1)
 
 
client_print(id,print_chat,"Global JobID: %d",SQL_ResultInt)
 
 return 
PLUGIN_HANDLED

PHP Code:
public SQL_QueryHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
 if(
FailState == TQUERY_CONNECT_FAILED)
  return 
set_fail_state("Could not connect to SQL database.")
 else if(
FailState == TQUERY_QUERY_FAILED)
  return 
set_fail_state("Query failed.")
 
 if(
Errcode)
  return 
log_amx("Error on query: %s",Error)
   
   new 
Number=250
   
   
while(SQL_MoreResults(Query))
   {
    
Number=SQL_ReadResult(Query,0)
    
    
client_print(0,print_chat,"Local JobID: %d",Number)
    
    
SQL_NextRow(Query)
   }
   
   
SQL_ResultInt=Number
 
 
return PLUGIN_CONTINUE

Ignore the strange complexity of the functions calling functions and blah blah, I was trying stuff to try and get this to work.

Note: Global JobID print is 500, local is 1.

1 is the correct JobID.
Bad_Bud is offline
harbu
Senior Member
Join Date: Aug 2004
Location: Finland
Old 01-07-2007 , 09:43   Re: SQLx to global variable problem
Reply With Quote #2

TSRP?
__________________
harbu is offline
Send a message via MSN to harbu
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 01-07-2007 , 16:02   Re: SQLx to global variable problem
Reply With Quote #3

Yeah, but any idea why I can't save the SQL results in a global variable?
Bad_Bud is offline
lunarwolfx
Member
Join Date: Feb 2005
Old 01-07-2007 , 20:34   Re: SQLx to global variable problem
Reply With Quote #4

To pass a player's id into the function.

Add this

Code:
new data[2]; format(data,1,"%d",id) SQL_ThreadQuery(SQL_Tuple,"QueryHandle",Query,data,1)

Then in your Query Handle, put this somewhere after the error checking:

Code:
new id = str_to_num(data)

and you now have the id of the player.

And try putting SQL_ResultInt=number inside your loop instead of after it.

Last edited by lunarwolfx; 01-07-2007 at 20:37.
lunarwolfx is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 01-07-2007 , 21:39   Re: SQLx to global variable problem
Reply With Quote #5

Actually, I forgot to mention that I had SQL_ResultInt inside the loop instead of outside of the loop and it still gave the wrong results. One strange thing to note is the fact that when I printed out ResultInt inside of the SQL function, it returned 250, the number I assigned to Number before the loop.

Anyway, thanks for showing me how to pass things in. Hopefully I won't have anymore problems... I'm going to go ahead and remove the loop, seeing as it's causing some problems... and I'm not trying to retrieve more than one piece of data at a time.

THANK YOU SO MUCH... I have been waiting for a response for quite some time.

Where is the documentation on SQLx other than Hawk's tutorial? I heard Bailopan wrote something about it, but the only help I got from bail was when I read inside of SQLx.inc...

[Edit] Oh, and data[] would have to be size [3], because some id's are two digits long...

Also, can you keep adding data to the end of the threadquery function, such as two player id's at once?

Last edited by Bad_Bud; 01-08-2007 at 00:07.
Bad_Bud is offline
lunarwolfx
Member
Join Date: Feb 2005
Old 01-08-2007 , 00:33   Re: SQLx to global variable problem
Reply With Quote #6

you can, but you'll have to parse it.

As far as documentation goes, the best I found was hawk's tutorial.

and thx for the heads up about the length.
lunarwolfx is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 01-09-2007 , 01:11   Re: SQLx to global variable problem
Reply With Quote #7

UGH!

With the passing in of the id, which really makes things a whole lot simpler, I still have the problem with the global variables!

The data stays intact inside the SQL function, but then manages to disappear the second the function returns...

It's quite... sucky. Anybody know how to get SQLx data into global variables? This is quite the sucky predicament.
Bad_Bud is offline
lunarwolfx
Member
Join Date: Feb 2005
Old 01-09-2007 , 21:59   Re: SQLx to global variable problem
Reply With Quote #8

Hmm... well storing things work fine for me, are you positively sure your variables are global?
lunarwolfx is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 01-09-2007 , 22:13   Re: SQLx to global variable problem
Reply With Quote #9

Yeah. I give the global variable the data in the SQL query handler, and even print it out right before the PLUGIN_CONTINUE is returned, yet a line after the threadquery, I'll display the same global variable (which is not passed into the function, just used), and it will be the same as it was before it was given a new value in the ThreadQuery handler.

[Edit] I figured out what the problem is... and I'm not sure what to do about it. The data is being retrieved and stored correctly, but in the function I was using (since SQLx is threaded), the function expected the database to be done before the next line of code was executed, and this was not the case.

So... How would I get around this problem?

It will cause problems for things such as when a user opens a list of items that are retrieved from the database right before they are displayed. If this problem continues for that situation, they'll have a blank list of items. Is there any way to tell a function to wait for the threaded query to return a result before continuing?

I need my plugin to act synchronous, even though threaded queries are not. I want this to be able to run smoothely on laggy servers, but I still need to know when data has been retrieved, because otherwise, I have some big problems.

Last edited by Bad_Bud; 01-09-2007 at 23:52.
Bad_Bud is offline
Old 01-09-2007, 23:05
Bad_Bud
This message has been deleted by Bad_Bud. Reason: double post
Reply



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

Forum Jump


All times are GMT -4. The time now is 22:26.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode