AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   dbi_free_result: Invalid DBI result handle (https://forums.alliedmods.net/showthread.php?t=54977)

Teyut 05-09-2007 18:39

dbi_free_result: Invalid DBI result handle
 
Hi,

The following code is supposed to retrieve some stuffs from my database using AMXX v1.76a:
Code:
// The SQL query retrieve the 15 best players average XP new szQuery[256]; format(szQuery, 255, "SELECT AVG(`xp`) as `xp_avg` FROM \         (SELECT `xp` FROM `%s` ORDER BY `xp` DESC LIMIT 0, 15) \     AS `best_players`",     g_DBTableName); new Result:res = dbi_query(sql, szQuery); // Make sure we have a result if (res <= RESULT_NONE) {     server_print("%s An error has occurred when retrieving the best \         players XP average.", g_MODclient);     XP_DBI_Error( res, szQuery, 0 );     return PLUGIN_CONTINUE; } if(res && dbi_nextrow(res)) {     new szXP[8], iXP, iLevel;     dbi_result(res, "xp_avg", szXP, 7);     iXP = str_to_num( szXP );         // Compute the level corresponding to these XPs     for(new i = 0 ; i <= 10 ; ++i) {         if(iXP >= xplevel[i])             iLevel = i         else             break     }         // Set multiplier values for each level under the (average) best one     new iLevelDiff = 10 - iLevel     while(iLevel >= 0) {         xpmultiplier[iLevel] = multiplierdata[iLevel + iLevelDiff]         iLevel--     } } // Free the result set dbi_free_result(res);

The connection to the DB works, and the query doesn't fail. Most of the time, there is a row result (i.e., when the table isn't empty). The problem is at the last line ; every time it's executed, I've got a message in the error log:
Code:

[MySQL] Invalid DBI result handle 54
[AMXX] Displaying debug trace (plugin "warcraft3FT.amxx")
[AMXX] Run time error 10: native error (native "dbi_free_result")
[AMXX]    [0] XP.inl::XP_Set_Multiplier (line 315)
[AMXX]    [1] XP.inl::XP_Set (line 179)
[AMXX]    [2] war3ft.inl::WAR3_Set_Variables (line 1221)

It's weird, because if the handle was invalid, an almost identical error message should have been written when the dbi_result function is called ! The only difference between dbi_free_result and dbi_result is the way the result handle is given (by reference for the former, by value for the later). However, there're other queries that are made in almost the same way (dbi_query->dbi_nextrow->dbi_result->dbi_free_result) to the same table in the plugin, and no error message is present in the error log related to these other queries, so I really don't understand why I've got this issue :(

If you can explain me the reason for that error message, please do it, because I'm really tired of that error log pollution :| Thanks in advance :wink:

Greenberet 05-10-2007 02:27

Re: dbi_free_result: Invalid DBI result handle
 
as far as i know your sql qry is false.
You can't use an subselect to get an tablename.

Teyut 05-10-2007 04:17

Re: dbi_free_result: Invalid DBI result handle
 
Quote:

Originally Posted by Greenberet (Post 475228)
You can't use an subselect to get an tablename.

As said above, there's no problem with the SQL query: I've tried it using phpadmin, and confirmed it works in my plugin. That may be a MySQL specific feature, but subqueries in the FROM clause work (and it's not a tablename selection).

Brad 05-10-2007 09:09

Re: dbi_free_result: Invalid DBI result handle
 
Change your RESULT_NONE to RESULT_FAIL. See if that helps.

Teyut 05-10-2007 21:30

segfault in dbi_result ?
 
I've tried, but actually, since RESULT_FAILED < RESULT_NONE, it didn't change anything :(

I've changed the SQL query and added a few debug output:
Code:
        // Make sure we have a valid SQL Connection         if(!XP_Check_Connection()){             return PLUGIN_CONTINUE;         }             // The SQL query retrieve the 15 best players XP average         new szQuery[256]; //      format(szQuery, 255, "SELECT AVG(`xp`) as `xp_avg` FROM \ //              (SELECT `xp` FROM `%s` ORDER BY `xp` DESC LIMIT 0, 15) \ //          AS `best_players`", g_DBTableName);         format(szQuery, 255, "SELECT MAX(`xp`) as `xp_avg` FROM `%s`",             g_DBTableName);         log_amx("QUERY = %s", szQuery);         new Result:res = dbi_query(sql, szQuery);         log_amx("RESULT = %d", res);                 // Make sure we have a result         if (res <= RESULT_FAILED) {             log_amx("res <= RESULT_FAILED");             server_print("%s An error has occurred when retrieving \                 the best players XP average.", g_MODclient);             XP_DBI_Error( res, szQuery, 0 );             return PLUGIN_CONTINUE;         }         if(res && dbi_nextrow(res)) {             log_amx("dbi_nextrow(res) => OK");             new szXP[8], iXP, iLevel;             dbi_result(res, "xp_avg", szXP, 7);             log_amx("xp_avg == %s", szXP);             iXP = str_to_num( szXP );                     /* [SNIP] */         }                 log_amx("RESULT = %d", res);         // Free the result set         dbi_free_result(res);
With that code, there's no error message in the error log, and the debug outputs look like the following:
Code:

QUERY = SELECT MAX(`xp`) as `xp_avg` FROM `war3FTusers`
RESULT = 5
dbi_nextrow(res) => OK
xp_avg == 36045
RESULT = 5

Now, if I go back to the original SQL query, the error message comes again, and the debug outputs is a bit different:
Code:

QUERY = SELECT AVG(`xp`) as `xp_avg` FROM (SELECT `xp` FROM `war3FTusers` ORDER BY `xp` DESC LIMIT 0, 15) AS `best_players`
RESULT = 5
dbi_nextrow(res) => OK
xp_avg == 27595.7333
RESULT = 51

That means that the call to dbi_result has changed the value of the result handle ... and that is not expected, since the handle is given by value to that function !!? It looks like a stack issue then, and it is, because the size of the string in szXP is larger than 8 which is the size allocated for the szXP buffer ! The last parameters of the dbi_result function is supposed to be the maximum size of character to write to the given buffer, but it seems it doesn't work well here.

Maybe my problem is related to this thread ? But I didn't find more details about that segfault in the AMXX's flyspray :|

Using the 3 parameters form of dbi_result (with a float reference instead of a buffer), there's no error message anymore, so I guess it will work better this way ...

Since it looks like my bug isn't a scripting one anymore, I guess I should not post anything more in that forum. BTW, thanks for the help ;)


All times are GMT -4. The time now is 06:43.

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