Raised This Month: $12 Target: $400
 3% 

Solved [MySQL] Return function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wilian159
Member
Join Date: Dec 2013
Old 01-08-2022 , 23:09   [MySQL] Return function
Reply With Quote #1

is there any way i can return integer values or text according to a query?


we usually do this

PHP Code:
public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'")
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....

    
new id

    
if(SQL_NumResults(query))
    {
        
id SQL_ReadResult(xQuery0)
    }
    else
    {
        
id = -1
    
}

I wanted to do something like that.:

return straight into function

PHP Code:
public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"XXXXXX""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'")

    new 
id

    
if(SQL_NumResults(query))
    {
        
id SQL_ReadResult(xQuery0)
    }
    else
    {
        
id = -1
    
}

    return 
id


like

PHP Code:
new user_id SqlGetUserIndex(id)
out
there must be a way? or it is not possible to do this at the moment.
__________________

Last edited by wilian159; 01-09-2022 at 12:10.
wilian159 is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 01-09-2022 , 03:47   Re: [MySQL] Return function
Reply With Quote #2

Instead of using SQL_ThreadQuery, use SQL_Execute. You will.need to prepare the query using Sql_PrepareQuery
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-09-2022 , 08:44   Re: [MySQL] Return function
Reply With Quote #3

Quote:
Originally Posted by Shadows Adi View Post
Instead of using SQL_ThreadQuery, use SQL_Execute. You will.need to prepare the query using Sql_PrepareQuery
You should mention that this will execute the queries in synchronous/blocking mode which will block the main thread until the query is completed. Generally, this isn't something you want to do.

Threaded queries are better to use in a live game for this reason, but due to their asynchronous nature, one can not do what OP wants because when the SQL_ThreadQuery returns there is no guarantee the query was actually executed. The callback is meant to serve as a notification that the query is done and results are available.
__________________
HamletEagle is offline
wilian159
Member
Join Date: Dec 2013
Old 01-09-2022 , 09:56   Re: [MySQL] Return function
Reply With Quote #4

Quote:
Originally Posted by HamletEagle View Post
You should mention that this will execute the queries in synchronous/blocking mode which will block the main thread until the query is completed. Generally, this isn't something you want to do.

Threaded queries are better to use in a live game for this reason, but due to their asynchronous nature, one can not do what OP wants because when the SQL_ThreadQuery returns there is no guarantee the query was actually executed. The callback is meant to serve as a notification that the query is done and results are available.
so there's no way to do what I want?


does this make no sense here?

PHP Code:
new VarTest

public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'")

    return 
VarTest
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....

    
if(SQL_NumResults(query))
    {
        
VarTest SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest = -1
    
}

__________________

Last edited by wilian159; 01-09-2022 at 10:02.
wilian159 is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 01-09-2022 , 10:20   Re: [MySQL] Return function
Reply With Quote #5

Quote:
Originally Posted by wilian159 View Post
so there's no way to do what I want?


does this make no sense here?

PHP Code:
new VarTest

public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'")

    return 
VarTest
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....

    
if(SQL_NumResults(query))
    {
        
VarTest SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest = -1
    
}

Yes, there is a way, using SQL_Execute. HamletEagle explained the difference between this and SQL_ThreadQuery.

No, it won't work, because the callback is executed after the SqlGetUserIndex() function ends. Test it by yourself by adding debug messages.

PHP Code:
new VarTest

public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'")
    
server_print("SqlGetUserIndex() function")

    return 
VarTest
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....

    
if(SQL_NumResults(query))
    {
        
VarTest SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest = -1
    
}

    
server_print("_SqlGetUserIndex() function")

__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-09-2022 , 10:22   Re: [MySQL] Return function
Reply With Quote #6

No, it is not correct. Let me try to explain again using your code:

PHP Code:
new VarTest

public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'"//when this finishes executing the query is not guaranteed to be completed

    
return VarTest //this can be executed BEFORE _SqlGetUserIndex has a chance to be called and set VarTest, it will likely return 0 or the result from a previous query

__________________
HamletEagle is offline
wilian159
Member
Join Date: Dec 2013
Old 01-09-2022 , 10:24   Re: [MySQL] Return function
Reply With Quote #7

Quote:
Originally Posted by Shadows Adi View Post
Yes, there is a way, using SQL_Execute. HamletEagle explained the difference between this and SQL_ThreadQuery.

No, it won't work, because the callback is executed after the SqlGetUserIndex() function ends. Test it by yourself by adding debug messages.

PHP Code:
new VarTest

public SqlGetUserIndex(id)
{
    
SQL_ThreadQuery(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'")
    
server_print("SqlGetUserIndex() function")

    return 
VarTest
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....

    
if(SQL_NumResults(query))
    {
        
VarTest SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest = -1
    
}

    
server_print("_SqlGetUserIndex() function")

if I use 'SQL_Execute' there will be some delays in the game


haven't tested it, would that work?:
PHP Code:
new VarTest[33]

public 
MyFunction(id)
{
    
// Query
    
SqlGetUserIndex(id)

    new 
res SqlReturnQuery(id)
    
    if(
res != -1)
    {
        
// result int
    
}
}

public 
SqlGetUserIndex(id)
{
    
VarTest[id] = -1

    
new data[1]
    
data[0] = id

    SQL_ThreadQuery
(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'"datasizeof(data))
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....

    
new id data[0]

    if(
SQL_NumResults(query))
    {
        
VarTest[id] = SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest[id] = -1
    
}

    
SqlReturnQuery(id)
}

public 
SqlReturnQuery(id)
{
    return 
VarTest[id]

__________________
wilian159 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-09-2022 , 11:40   Re: [MySQL] Return function
Reply With Quote #8

No, because immediately after calling SqlGetUserIndex(), you are expecting to have the return value from the threaded query. As HamletEagle said, the query is asynchronous, meaning you can think if it as being added to a queue and executed when resources are available. You cannot expect results to be immediately available.
PHP Code:
new VarTest[33]

public 
MyFunction(id)
{
    
// Query
    
SqlGetUserIndex(id)
}

public 
SqlGetUserIndex(id)
{
    
VarTest[id] = -1
    
    
new data[1]
    
data[0] = id
    
    SQL_ThreadQuery
(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'"datasizeof(data))
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....
    
    
new id data[0]
    
    if(
SQL_NumResults(query))
    {
        
VarTest[id] = SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest[id] = -1
        
//Do what you want here that you would do in your code above when the value = -1
    
}

__________________

Last edited by Bugsy; 01-09-2022 at 11:47.
Bugsy is offline
wilian159
Member
Join Date: Dec 2013
Old 01-09-2022 , 12:10   Re: [MySQL] Return function
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
No, because immediately after calling SqlGetUserIndex(), you are expecting to have the return value from the threaded query. As HamletEagle said, the query is asynchronous, meaning you can think if it as being added to a queue and executed when resources are available. You cannot expect results to be immediately available.
PHP Code:
new VarTest[33]

public 
MyFunction(id)
{
    
// Query
    
SqlGetUserIndex(id)
}

public 
SqlGetUserIndex(id)
{
    
VarTest[id] = -1
    
    
new data[1]
    
data[0] = id
    
    SQL_ThreadQuery
(handle"_SqlGetUserIndex""SELECT id FROM users WHERE authid = 'STEAM_XXXXXXXX'"datasizeof(data))
}

public 
_SqlGetUserIndex(fail_stateHandle:queryerror[], error_codedata[], data_size)
{
    
// check fails, etc....
    
    
new id data[0]
    
    if(
SQL_NumResults(query))
    {
        
VarTest[id] = SQL_ReadResult(xQuery0)
    }
    else
    {
        
VarTest[id] = -1
        
//Do what you want here that you would do in your code above when the value = -1
    
}

I get it, I'll use the standard method
__________________
wilian159 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-10-2022 , 22:35   Re: [MySQL] Return function
Reply With Quote #10

Not sure what the standard method means, but you want to avoid using an execute query as it will cause lags, if this is what you are referring to.

I gave you an idea in my code snippet. You can execute the handler function once the result is determined:
Code:
public _SqlGetUserIndex(fail_state, Handle:query, error[], error_code, data[], data_size) {     // check fails, etc....         new id = data[0]         if(SQL_NumResults(query))     {         VarTest[id] = SQL_ReadResult(xQuery, 0)
        UserFound( id );
    }     else     {         VarTest[id] = -1
        UserNotFound( id );
    } }
__________________
Bugsy is offline
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

Forum Jump


All times are GMT -4. The time now is 17:20.


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