Issue information

Issue ID
#5937
Status
New
Severity
None
Started
Hercules Elf Bot
Jun 5, 2012 5:28
Last Post
Hercules Elf Bot
Jun 5, 2012 15:58
Confirmation
N/A

Hercules Elf Bot - Jun 5, 2012 5:28

Originally posted by [b]Senshuken[/b]
When running a select query if the query fails, no value gets pushed to the variable that is specified. A problem arises when the following is done:

[CODE]
prontera,155,175,4 script test 58,{
query_sql ( "SELECT `account_id` FROM `login` WHERE `userid` = '" + escape_sql ( "eevan" ) + "'", @a$ );
dispbottom @a$;
query_sql ( "SELECT `account_id` FROM `login` WHERE `userid` = '" + escape_sql ( "fasdf" ) + "'", @a$ );
dispbottom @a$;
end;
}
[/CODE]

Assuming that eevan is a valid userid and fasdf not a valid one.

The first query will display some account_id lets say 2000001 and the second one will display the same account_id even though the second one is an invalid userid.

Shouldn't it push 0 or "" to the variable when query fails?

Hercules Elf Bot - Jun 5, 2012 6:24

Originally posted by [b]EvilPuncker[/b]
why don't just use different variables for each query? also account_id table is used to store integer data, so you don't need the $ postfix

Hercules Elf Bot - Jun 5, 2012 6:58

Originally posted by [b]Senshuken[/b]
[quote name='EvilPuncker' timestamp='1338877470' post='10095']
why don't just use different variables for each query? also account_id table is used to store integer data, so you don't need the $ postfix
[/quote]

Well this is just an example, my script loops through an array of player names and then does the same query. The simple fix is to reset the value before querying, but just thought it was weird that it doesn't reset it if it doesn't find anything.
PS: I know it doesn't need $, it was originally written without it, but I was testing it with different variable types see if it made a difference (it didn't).

Hercules Elf Bot - Jun 5, 2012 7:39

Originally posted by [b]Xantara[/b]
Hmm.. I kinda agree that it should push a 0 or an empty string to the variable(s) when the query produces 0 rows (provided that it didn't error). But i'll wait for the other devs for their input.

Also, as a note, there is a way to check if it did result to 0 rows such that you can then do a manual reset:
[CODE]
set .@numRows, query_sql ( "SELECT `account_id` FROM `login` WHERE `userid` = '" + escape_sql ( "username" ) + "'", .@id );
if(.@numRows == 0) {
set .@id, 0;
dosomething;
}
[/CODE]

This post has been edited by Xantara on Jun 5, 2012 8:10

Hercules Elf Bot - Jun 5, 2012 10:20

Originally posted by [b]Toshiro[/b]
You shouldn't only rely on the variables you specify for the results, but rather on the return value of query_sql.

Let's take a different example than querying the account id: we want to query the `char`.`fame` column.
This actually can be 0 for the result, so just checking the variables if you got any results is just wrong, as *Athena handles 0-variables the same as not existing ones (the same goes for empty string variables). That means, it doesn't care if it doesn't exist (or never was set by anything) and just uses 0 (or an emptry string in case of string variables).
While you'd get the same result when processing .@id (containing 0), e.g. arraylength can return a lower value than actually were returned by query_sql.
Therefore when processing the results of query_sql you need to rely on the actual return value of query_sql which is the real amount of rows returned by the query.

Hercules Elf Bot - Jun 5, 2012 15:58

Originally posted by [b]Brian[/b]
I agree with Toshiro, scripts should be written to use the return value of query_sql.

I also have a suggestion: change query_sql to return -1 if it fails (right now it returns 0).
-1 was previously used as the "query failed" return value ([i]In the TXT version it doesn't fill the array and always return -1[/i]).

This post has been edited by Brian on Jun 5, 2012 16:33