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

Return integer from mysql handler function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 01-05-2022 , 12:58   Return integer from mysql handler function
Reply With Quote #1

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 }
bigdaddy424 is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 01-05-2022 , 14:07   Re: Return integer from mysql handler function
Reply With Quote #2

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)
    }
}
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 01-05-2022 at 14:07.
JocAnis is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 01-05-2022 , 14:30   Re: Return integer from mysql handler function
Reply With Quote #3

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
bigdaddy424 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-06-2022 , 00:31   Re: Return integer from mysql handler function
Reply With Quote #4

Quote:
Originally Posted by bigdaddy424 View Post
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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-06-2022 , 13:51   Re: Return integer from mysql handler function
Reply With Quote #5

Quote:
Originally Posted by bigdaddy424 View Post
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).
__________________

Last edited by HamletEagle; 01-06-2022 at 13:53.
HamletEagle is offline
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 15:13.


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