AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Return integer from mysql handler function (https://forums.alliedmods.net/showthread.php?t=335809)

bigdaddy424 01-05-2022 12:58

Return integer from mysql handler function
 
Running this code returns will always retrurn 1 even though the table has more than 1000 entries.
Code:
#include <amxmodx> #include <sqlx> new MYSQL_HOST[] = "" new MYSQL_USER[] = "" new MYSQL_PASSWORD[] = "" new MYSQL_DATABASE[] = "" new MYSQL_TABLE[] = "" new Handle: MYSQL_CONNECTION public plugin_init(){     register_srvcmd("CountEntries", "CountEntries") } public plugin_cfg(){     MYSQL_CONNECTION = SQL_MakeDbTuple(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE) } public CountEntries(){     new query[64]     formatex(query, charsmax(query), "SELECT COUNT(*) FROM `%s`;", MYSQL_TABLE)     new entries = SQL_ThreadQuery(MYSQL_CONNECTION, "SQLCountEntries", query)     server_print("entries[%d]", entries) } public SQLCountEntries(failState, Handle:query, error[], errNum){     new entries = 0     if(SQL_NumResults(query)){         entries = SQL_ReadResult(query, 0)     }     return entries }

JocAnis 01-05-2022 14:07

Re: Return integer from mysql handler function
 
hello, can you test this and tell us results?

Code:

public CountEntries(){
    new query[256]
    formatex(query, charsmax(query), "SELECT COUNT(*) FROM `%s`;", MYSQL_TABLE)
    SQL_ThreadQuery(MYSQL_CONNECTION, "SQLCountEntries", query)
   
}

public SQLCountEntries(failState, Handle:query, error[], errNum){
    new entries = 0
    if(SQL_NumResults(query)){
        entries = SQL_ReadResult(query, 0)
        server_print("entries[%d]", entries)
    }
}


bigdaddy424 01-05-2022 14:30

Re: Return integer from mysql handler function
 
Well now it works
I thought that the handler would return the same thing
Kind of irritating that you should do that inside the function

Natsheh 01-06-2022 00:31

Re: Return integer from mysql handler function
 
Quote:

Originally Posted by bigdaddy424 (Post 2767726)
Well now it works
I thought that the handler would return the same thing
Kind of irritating that you should do that inside the function

Read the documentation of SQL_ThreadQuery to understand how it works.

HamletEagle 01-06-2022 13:51

Re: Return integer from mysql handler function
 
Quote:

Originally Posted by bigdaddy424 (Post 2767726)
Well now it works
I thought that the handler would return the same thing
Kind of irritating that you should do that inside the function

The function can not return anything useful because when it returns the results may not be available. The function is non-blocking, it puts a request saying that you want the query to be executed but does not wait for the execution to finish and instead delegates the work to a worker thread. This has the advantage of not blocking the main game thread if the connection takes a while, the query takes a long time to execute, etc. When the query eventually finishes executing, a notification is sent to let you know the results are ready.

If you want blocking behavior, consider using non-threaded natives(you probably should not).


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

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