Raised This Month: $32 Target: $400
 8% 

Solved Issue with conditionals during a SQL query


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
r3D w0LF
Senior Member
Join Date: Dec 2013
Location: Albania
Old 09-19-2018 , 14:24   Issue with conditionals during a SQL query
Reply With Quote #1

So, i am having a hard time dealing with SQL queries.

This is the first part (im using SQL_TConnect to connect on plugin start):
PHP Code:
    char mainQuery[512];
    
Format(mainQuerysizeof(mainQuery), "SELECT entry FROM table WHERE steamID LIKE '%s'"szAuth);
    
SQL_TQuery(g_hEntriesDbcheckEntrymainQueryclient); 
And this is the checkEntry part:

PHP Code:
public checkEntry(Handle:ownerHandle:handle, const String:error[], any:data)
{
    if(
handle == INVALID_HANDLE)
    {
        
PrintToConsole(data"Error searching in database: %s"error);
        return;
    }

    
char question[255];
    
int i 0;
    while(
SQL_FetchRow(handle))
    {
        
SQL_FetchString(handle0questionsizeof(question));
        
        for(new 
j<= sizeof(g_serverEntry); j++)
        {
            if(
strlen(g_serverEntry[j]))
            {
                if(
StrEqual(g_serverEntry[j], question) == true)
                {
                    
PrintToConsole(data"Entry: %s has been found"question);
                } else
                {
                    
PrintToConsole(data"Entry: %s missing"question);
                }
                
            } else
            {
                break;
            }
        }
        
i++;
    }

This returns:
Code:
Entry: E1 has been found
Entry: E1 missing
Entry: E2 missing
Entry: E2 has been found
However when omitting
PHP Code:
 else
{
    
PrintToConsole(data"Entry: %s missing"question);

It seems to run fine. Maybe is there something I am doing wrong?

Last edited by r3D w0LF; 09-21-2018 at 18:40.
r3D w0LF is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 09-19-2018 , 15:38   Re: Issue with conditionals during a SQL query
Reply With Quote #2

"SELECT entry FROM table WHERE steamID LIKE '%s'", szAuth

I'd say that is your main problem (haven't really checked the rest of the code that well).

What the query tells me is; find anything in the steamID column that ends with an s.

To any of you out there... no, it doesn't matter if it's a string or not.

Replace LIKE with =
mug1wara is offline
r3D w0LF
Senior Member
Join Date: Dec 2013
Location: Albania
Old 09-19-2018 , 15:45   Re: Issue with conditionals during a SQL query
Reply With Quote #3

Quote:
Originally Posted by mug1wara View Post
"SELECT entry FROM table WHERE steamID LIKE '%s'", szAuth

I'd say that is your main problem (haven't really checked the rest of the code that well).

What the query tells me is; find anything in the steamID column that ends with an s.

To any of you out there... no, it doesn't matter if it's a string or not.

Replace LIKE with =
isnt that how you prepare/format a query? (half-prepare anyway)
And no, = and LIKE didnt make a difference:

Code:
Entry: E1 missing
Entry: E2 missing
Entry: E1 has been answered
Entry: E2 has been answered
Yes, they have switched places this time, but thats because i changed the order they print in console.

EDIT: Here is the missing line

char szAuth[32];
GetClientAuthId(client, AuthId_Steam2, szAuth, sizeof(szAuth));

But either way, if the query was not formatted correctly i think it wouldnt return anything.

And also, the queries are in a remote database

Last edited by r3D w0LF; 09-19-2018 at 15:47.
r3D w0LF is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-19-2018 , 18:13   Re: Issue with conditionals during a SQL query
Reply With Quote #4

It's not clear what your problem is without knowing the data.

If your query returns two strings, "E1" and "E2", and your g_serverEntry array also has two non-empty values that are "E1" and "E2", then I would expect your function to print out exactly what you said it did in your first post.
Fyren is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-19-2018 , 18:46   Re: Issue with conditionals during a SQL query
Reply With Quote #5

LIKE doesn't do partial matches unless you use wildcards to tell it to. In SQL, _ matches a single character, and % matches any number of characters.

Bear in mind that to pass a % through a format function, you have to use %% instead.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 09-19-2018 at 18:47. Reason: Changed wording of _ description
Powerlord is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 09-20-2018 , 01:43   Re: Issue with conditionals during a SQL query
Reply With Quote #6

Could you PrintToServer g_serverEntry please?
mug1wara is offline
r3D w0LF
Senior Member
Join Date: Dec 2013
Location: Albania
Old 09-20-2018 , 07:52   Re: Issue with conditionals during a SQL query
Reply With Quote #7

Quote:
Originally Posted by mug1wara View Post
Could you PrintToServer g_serverEntry please?
I hardcoded it inside the plugin i am going to upload.

Here is a raw test plugin, the same issue persists, i thought this will be better than just posting pieces of the main plugin.
Same issue persists:

E1 and E2 are in database, server entries are E1, E2
Code:
Entry: E2 NOT found
Entry: E1 NOT found
Total results: 2
Entry: E1 found
Entry: E2 found
E3 and E4 are in database, server entries are E1, E2
Code:
Entry: E4 NOT found
Entry: E4 NOT found
Entry: E3 NOT found
Entry: E3 NOT found
Total results: 2
E2 and E3 are in database, server entries are E1, E2
Code:
Entry: E2 NOT found
Entry: E3 NOT found
Entry: E3 NOT found
Total results: 2
Entry: E2 found
EDIT: For some reason i replaced g_serverEntry with g_kvEntry

EDIT2: Also uploaded the database used for the plugin
Attached Files
File Type: sp Get Plugin or Get Source (rwttestUpload.sp - 152 views - 3.1 KB)
File Type: zip entries.zip (819 Bytes, 88 views)

Last edited by r3D w0LF; 09-20-2018 at 08:15.
r3D w0LF is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 09-20-2018 , 08:46   Re: Issue with conditionals during a SQL query
Reply With Quote #8

PHP Code:
    Format(g_kvEntry[0], sizeof(g_kvEntry), "E1");
    
Format(g_kvEntry[1], sizeof(g_kvEntry), "E2"); 
use sizeof(g_kvEntry[]). with your code g_kvEntry[0] will take size 10 and not 255, but this is not the problem.

PHP Code:
for(new j<= sizeof(g_kvEntry); j++) 
correct: j < sizeof(g_kvEntry)

you have some minor errors in your code, you should solve them and then solve the big problem.
Ilusion9 is offline
r3D w0LF
Senior Member
Join Date: Dec 2013
Location: Albania
Old 09-20-2018 , 11:06   Re: Issue with conditionals during a SQL query
Reply With Quote #9

Quote:
Originally Posted by Ilusion9 View Post
PHP Code:
    Format(g_kvEntry[0], sizeof(g_kvEntry), "E1");
    
Format(g_kvEntry[1], sizeof(g_kvEntry), "E2"); 
use sizeof(g_kvEntry[]). with your code g_kvEntry[0] will take size 10 and not 255, but this is not the problem.

PHP Code:
for(new j<= sizeof(g_kvEntry); j++) 
correct: j < sizeof(g_kvEntry)

you have some minor errors in your code, you should solve them and then solve the big problem.
Yeah, thanks

Regarding this thread, nvm, i figured it out. Could mark it as solved.
r3D w0LF is offline
Reply


Thread Tools
Display Modes

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 13:39.


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