AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved [MySQL] Return function (https://forums.alliedmods.net/showthread.php?t=335859)

wilian159 01-08-2022 23:09

[MySQL] Return function
 
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.

Shadows Adi 01-09-2022 03:47

Re: [MySQL] Return function
 
Instead of using SQL_ThreadQuery, use SQL_Execute. You will.need to prepare the query using Sql_PrepareQuery

HamletEagle 01-09-2022 08:44

Re: [MySQL] Return function
 
Quote:

Originally Posted by Shadows Adi (Post 2768036)
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.

wilian159 01-09-2022 09:56

Re: [MySQL] Return function
 
Quote:

Originally Posted by HamletEagle (Post 2768052)
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
    
}



Shadows Adi 01-09-2022 10:20

Re: [MySQL] Return function
 
Quote:

Originally Posted by wilian159 (Post 2768060)
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")



HamletEagle 01-09-2022 10:22

Re: [MySQL] Return function
 
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



wilian159 01-09-2022 10:24

Re: [MySQL] Return function
 
Quote:

Originally Posted by Shadows Adi (Post 2768063)
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]



Bugsy 01-09-2022 11:40

Re: [MySQL] Return function
 
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
    
}



wilian159 01-09-2022 12:10

Re: [MySQL] Return function
 
Quote:

Originally Posted by Bugsy (Post 2768068)
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

Bugsy 01-10-2022 22:35

Re: [MySQL] Return function
 
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 );     } }


All times are GMT -4. The time now is 11:44.

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