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

Compare string readed from MySQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MindeLT
Senior Member
Join Date: Dec 2010
Location: Lithuania
Old 06-03-2011 , 06:43   Compare string readed from MySQL
Reply With Quote #1

Hi,

i'm trying to compare string readed from MySQL database row named Number. For example: i check user STEAM_ID, and then find a row with that auth. In that row there is number 5. Now i need to check it in script, is the number equal to 5. Example:
PHP Code:
new Handle:db Connect(); // Db Connect

decl String:auth[64];
decl String:query[512];
GetCmdArg(1arg1sizeof(arg1));
new 
target FindTarget(clientarg1,true,true);
GetClientAuthString(target,auth,sizeof(auth)); // Retrieving user STEAM_ID

Format(query,sizeof(query),"SELECT * FROM `number` WHERE `steam_id` = '%s'"auth); 
// Selecting data from row, where is user STEAM_ID;
SQL_Query(db,query);

new 
Handle:hQuery;
        
if ((
hQuery SQL_Query(dbquery)) == INVALID_HANDLE)
{
    
SQL_GetError(dberrorsizeof(error));
    
LogError("Shit happens. Error: %s"query);
    
LogError("Query error: %s"error);
    return 
Plugin_Handled;
}

if(
SQL_GetRowInt(hQuery) == 5)) // Checking data, is number equal to number 5.
{
    
PrintToChat(client" Succes, number in MySQL row is %s"number); // Succes
}
else
{
    
PrintToChat(client" Epic FAIL. "); // Dude, you're total dumbass.

So my question: What's the function to compare/check is MySQL RowInt, in this case, row name is: Number(row where is user STEAM_ID) equal to my typed number?

Last edited by MindeLT; 06-03-2011 at 06:48.
MindeLT is offline
Send a message via Skype™ to MindeLT
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 06-03-2011 , 08:52   Re: Compare string readed from MySQL
Reply With Quote #2

There's no SQL_GetRowInt function? SQL_GetRowCount does though, but i doubt that's what you want. That function returns the number of rows in your result, not the info inside them.

There is one call to SQL_Query(db,query); too much btw.

You may better specify the table fields explicitly in your query for a better overview. Only using SELECT * FROM... doesn't says clearly in what order the fields are fetched.

If there's only one row for each steamid in your table, you need to call SQL_FetchRow(hQuery); and compare the number returned with 5 like
SQL_FetchInt(hQuery, 0) == 5

You may just post the structure of your "numbers" table?
__________________
Peace-Maker is offline
MindeLT
Senior Member
Join Date: Dec 2010
Location: Lithuania
Old 06-03-2011 , 10:24   Re: Compare string readed from MySQL
Reply With Quote #3

Thank's for quick answer/

Code:
too much btw.
You mean, string size? 512 bytes?

I edited my code a little bit, creating couple of strings, and then comparing strings with StrEqual.

Table structure:
[IMG]http://img685.**************/img685/5627/warnw.png[/IMG]

Uploaded with **************
Btw, I'm creating WARN-system(Administrator can add warn to player, after player collects 5 warns, he get banned for 1 week);

New version:

PHP Code:
public Action:Command_Warn(clientargs)
{
    if (
GetConVarInt(warn_sistema) == 1)
    {    
        if (
args 1)
        {
            
ReplyToCommand(client"\x03[WARN] Command: /warn <nick> <reason>");
            return 
Plugin_Handled;
        }
    
        new 
Handle:db Connect();
    
        if (
db == INVALID_HANDLE)
        {
            
ReplyToCommand(client"[SM] %t""Could not connect to database");
            return 
Plugin_Handled;
        }
    
        
decl String:query[512];
        
decl String:arg1[32];
        
decl String:reason[32];
        
decl String:name[MAX_NAME_LENGTH];
        
decl String:error[255];
    
        
GetCmdArg(1arg1sizeof(arg1));
        new 
target FindTarget(clientarg1,true,true);
    
        
GetClientName(target,name,sizeof(name));
        
decl String:auth[64];
        
GetClientAuthString(target,auth,sizeof(auth));
        
        
GetCmdArg(2reasonsizeof(reason));
        
        
Format(query,sizeof(query),"SELECT warn FROM user_warn WHERE steam_id = '%s'"auth);
        
SQL_Query(db,query);
        
        new 
Handle:hQuery;
        
        if ((
hQuery SQL_Query(dbquery)) == INVALID_HANDLE)
        {
            
SQL_GetError(dberrorsizeof(error));
            
LogError("WARN system [MySQL] error: %s"query);
            
LogError("Query error: %s"error);
            return 
Plugin_Handled;
        }
        
            while (
SQL_FetchRow(hQuery))
            {
                
SQL_FetchString(hQuery0warnsizeof(warn));
                
            }
        new 
String:warn_0[3] = "0";
        new 
String:warn_1[3] = "1";
        new 
String:warn_2[3] = "2";
        new 
String:warn_3[3] = "3";
        new 
String:warn_4[3] = "4";
            
                if(
StrEqual(warnwarn_0))
                {
                    
Format(querysizeof(query), "INSERT INTO user_warn (nick, steam_id, warn) VALUES ('%s', '%s', '1') WHERE steam_id = '%s'"nameauthauth);
                    
SQL_Query(db,query);
                    
ReplyToCommand (client"\x03[WARN-0] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"namereasonauth);
                }
                else if(
StrEqual(warnwarn_1))
                { 
                    
Format(querysizeof(query), "UPDATE user_warn (nick, warn) VALUES ('%s', '2') WHERE steam_id = '%s'"nameauth);
                    
SQL_Query(db,query);
                    
ReplyToCommand (client"\x03[WARN-1] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"namereasonauth);
                }
                else if(
StrEqual(warnwarn_2))
                { 
                    
Format(querysizeof(query), "UPDATE user_warn (nick, warn) VALUES ('%s', '3') WHERE steam_id = '%s'"nameauth);
                    
SQL_Query(db,query);
                    
ReplyToCommand (client"\x03[WARN-2] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"namereasonauth);
                }
                else if(
StrEqual(warnwarn_3))
                { 
                    
Format(querysizeof(query), "UPDATE user_warn (nick, warn) VALUES ('%s', '4') WHERE steam_id = '%s'"nameauth);
                    
SQL_Query(db,query);
                    
ReplyToCommand (client"\x03[WARN-3] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"namereasonauth);
                }
                else if(
StrEqual(warnwarn_4))
                { 
                    
Format(querysizeof(query), "UPDATE user_warn (nick, warn) VALUES ('%s', '5') WHERE steam_id = '%s'"nameauth);
                    
SQL_Query(db,query);
                    
ReplyToCommand (client"\x03[WARN-4] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"namereasonauth);
                }
    
    }
    else
    {
        
ReplyToCommand (client"\x03[WARN] Warn system is offline.");
    }
    return 
Plugin_Continue;

I'd be very grateful, if you could look into code, and advise something.

At the moment, i'm having problem with adding data to mysql. For example:
PHP Code:
UPDATE user_warn (nickwarnVALUES ('%s''5'WHERE steam_id '%s'", name, auth);
// If you look to VALUES, i want to add number 5 into mysql, but don't really know any other way to do this. 

Last edited by MindeLT; 06-03-2011 at 10:33.
MindeLT is offline
Send a message via Skype™ to MindeLT
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 06-03-2011 , 12:27   Re: Compare string readed from MySQL
Reply With Quote #4

You don't have loop through the results, as there should be only one row for each steamid. You are able to compare with new strings directly - no need to declare new ones just for that StrEqual(warn, "1") etc, but you don't need that if statements at all.

You shouldn't add WHERE clause for INSERT statements in your query either.

Have a look at this. I don't get your idea of WARN-NUMBER in the ReplyToCommand() calls, as it's always one behind?

Please not that you should use threaded queries to avoid laggs, if your database has some higher respond times or is offline.

PHP Code:
public Action:Command_Warn(clientargs)
{
    if (
GetConVarInt(warn_sistema) == 1)
    {    
        if (
args 2)
        {
            
ReplyToCommand(client"\x03[WARN] Command: /warn <nick> <reason>");
            return 
Plugin_Handled;
        }
    
        new 
Handle:db Connect();
    
        if (
db == INVALID_HANDLE)
        {
            
ReplyToCommand(client"[SM] %t""Could not connect to database");
            return 
Plugin_Handled;
        }
    
        
decl String:query[512];
        
decl String:arg1[32];
        
decl String:reason[32];
        
decl String:name[MAX_NAME_LENGTH];
        
decl String:error[255];
    
        
GetCmdArg(1arg1sizeof(arg1));
        new 
target FindTarget(clientarg1,true,true);
    
        
GetClientName(target,name,sizeof(name));
        
decl String:auth[64];
        
GetClientAuthString(target,auth,sizeof(auth));
        
        
GetCmdArg(2reasonsizeof(reason));
        
        
Format(query,sizeof(query),"SELECT warn FROM user_warn WHERE steam_id = '%s'"auth);
        
        new 
Handle:hQuery;
        
        if ((
hQuery SQL_Query(dbquery)) == INVALID_HANDLE)
        {
            
SQL_GetError(dberrorsizeof(error));
            
LogError("WARN system [MySQL] error: %s"query);
            
LogError("Query error: %s"error);
            return 
Plugin_Handled;
        }
        
        
// This player wasn't warned before? Add him to the database!
        
if(!SQL_MoreRows(hQuery))
        {
            
Format(querysizeof(query), "INSERT INTO user_warn (nick, steam_id, warn) VALUES ('%s', '%s', 1)"nameauthauth);
            
SQL_Query(db,query);
            
ReplyToCommand (client"\x03[WARN-0] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"namereasonauth);
        }
        
// He's been warned before.. increase!
        
else
        {
            
decl String:warn[5];
            
SQL_FetchRow(hQuery)
            
SQL_FetchString(hQuery0warnsizeof(warn));
            
            new 
iWarn StringToInt(warn);
            
// Increase the warn level
            
iWarn++;
            
            
Format(querysizeof(query), "UPDATE user_warn (nick, warn) VALUES ('%s', %d) WHERE steam_id = '%s'"nameiWarnauth);
            
SQL_Query(db,query);
            
ReplyToCommand (client"\x03[WARN-%d] Succefuly added warn to player i: %s. Reason: %s. Steam_ID:%s"iWarn-1namereasonauth);
            
            if(
iWarn >= 5)
            {
                
BanClient(target0BANFLAG_AUTO);
                
// bla...
            
}
        }
    }
    else
    {
        
ReplyToCommand (client"\x03[WARN] Warn system is offline.");
    }
    return 
Plugin_Continue;

__________________
Peace-Maker is offline
MindeLT
Senior Member
Join Date: Dec 2010
Location: Lithuania
Old 06-03-2011 , 14:46   Re: Compare string readed from MySQL
Reply With Quote #5

I'm very grateful for your help.

Code:
I don't get your idea of WARN-NUMBER in the ReplyToCommand() calls, as it's always one behind?
I used this as indicator on server, to know, is warn is working/non warking.

Code:
You are able to compare with new strings directly - no need to declare new ones just for that StrEqual(warn, "1")
Thx for advise, i'll have it in-mind next time

About querries.

I always used SQL_Query(db, query), becouse it was the easiest way(rofl). Now i started to use SQL_FastQuery for INSERT, UPDATE, DELETE, DROP MySQL queries. Talking about thearded quaries, you have in mind SQL_TQuery? Create a new callback for this function, and execute a query there, right?

Again, thanks a lot for your help
MindeLT is offline
Send a message via Skype™ to MindeLT
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 06-03-2011 , 15:46   Re: Compare string readed from MySQL
Reply With Quote #6

Yes, have a read here.
http://wiki.alliedmods.net/SQL_%28So...g%29#Threading
__________________
Peace-Maker is offline
MindeLT
Senior Member
Join Date: Dec 2010
Location: Lithuania
Old 06-03-2011 , 16:13   Re: Compare string readed from MySQL
Reply With Quote #7

Thank's once more
MindeLT is offline
Send a message via Skype™ to MindeLT
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 06:22.


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