Raised This Month: $ Target: $400
 0% 

Simple SQL query to update points


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
rtk
Senior Member
Join Date: Dec 2009
Old 05-17-2014 , 19:36   Simple SQL query to update points
Reply With Quote #1

Firstly, I'm sorry to make such a dumb question.
I never got to play with SQL on SM before but I've been searching for some time and now I'm confused, specially with SQL_TQuery and callbacks.

All i need is a fast UPDATE query, so this should be easy.

In PHP it would look like this:
PHP Code:
function UpdatePlayerPoints($steamID64$amount)
{
    
$mysqli mysqli_connect(SQL_HOSTSQL_USERSQL_PWSQL_DB);
    if(!
$mysqli){
        return 
false;
    }

    
$result mysqli_query($mysqli"UPDATE `users` SET `points` = `points` + '{$amount}' WHERE `steamID64` = '{$sid64}'") or die(mysqli_error($mysqli));
    if(
$result){
        return 
true;
    } else {
        return 
false;
    }

    
mysqli_close($mysqli);

How would that look like ?

Thanks!
__________________
Never argue with an idiot. He lowers you to his level and then wins on experience!
rtk is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 05-17-2014 , 20:02   Re: Simple SQL query to update points
Reply With Quote #2

Take a look on that topic SQL (SourceMod Scripting)
LambdaLambda is offline
rtk
Senior Member
Join Date: Dec 2009
Old 05-17-2014 , 21:28   Re: Simple SQL query to update points
Reply With Quote #3

Nice example there, missed it.

So I came up with this:
PHP Code:
#include <sourcemod>
#include <morecolors>

public Plugin:myinfo =
{
    
name "Plugin Name",
    
author "Author",
    
description "",
    
version "1.0",
    
url ""
};

new 
Float:IntervalTime 60.0;
new 
NumPlaytime 1//minute(s)

public OnPluginStart()
{
    new 
Handle:kv CreateKeyValues("sql");
    
KvSetString(kv"driver""mysql");
    
KvSetString(kv"host""hostname");
    
KvSetString(kv"port""3306");
    
KvSetString(kv"database""databasename");
    
KvSetString(kv"user""sqluser");
    
KvSetString(kv"pass""xxxxxxxxxx");

    
decl String:error[255];
    
    new 
Handle:hDatabase SQL_ConnectCustom(kverrorsizeof(error), true);
    
    if (
hDatabase == INVALID_HANDLE)
    {
        
LogError("SQL Connection Failed: %s"error);
        return;
    }
    
CloseHandle(kv);
    
    
CreateTimer(IntervalTimeTimer_AddPlaytime_TIMER_REPEAT);
}

public 
Action:Timer_AddPlaytime(Handle:timer)
{
    for (new 
1<= MAXPLAYERS 1i++)
    {
        if (
IsClientConnected(i) && !IsFakeClient(i) && IsPlayerInGame(i))
        {
            new 
team GetClientTeam(i);
            if (
team == || team == 3)
            {
                
decl String:SteamID32[21];
                new 
String:CommunityID[18];
                
                
GetClientAuthString(iSteamID32sizeof(SteamID32));
                
GetCommunityIDString(SteamID32CommunityIDsizeof(CommunityID));
                
                new 
String:query[512];
                
Format(querysizeof(query), "UPDATE players SET playtime = playtime + %s WHERE SteamID64 = %s"NumPlaytimeCommunityID);
                
                if (!
SQL_FastQuery(hDatabasequery))
                {
                    new 
String:error[255]
                    
SQL_GetError(hDatabaseerrorsizeof(error))
                    
PrintToServer("Failed to query (error: %s)"error)
                }
            }
        }
    }

While compiling I get:
Code:
plugin.sp(58) : error 017: undefined symbol "hDatabase" // if (!SQL_FastQuery(hDatabase, query))
plugin.sp(61) : error 017: undefined symbol "hDatabase" // SQL_GetError(hDatabase, error, sizeof(error))
Yes, I'm sure I want to hardcore my sql credentials.

Help please.
__________________
Never argue with an idiot. He lowers you to his level and then wins on experience!

Last edited by rtk; 05-17-2014 at 21:29.
rtk is offline
Internet Bully
Member
Join Date: Apr 2014
Old 05-17-2014 , 21:43   Re: Simple SQL query to update points
Reply With Quote #4

You declare hDatabase in the scope of OnPluginStart.. It needs to be in the global scope the way you're using it. Read your errors, they tell you what's wrong...
__________________
Buy me a beer
or donate to Sourcemod
Taking some private plugin requests
Internet Bully is offline
rtk
Senior Member
Join Date: Dec 2009
Old 05-17-2014 , 22:05   Re: Simple SQL query to update points
Reply With Quote #5

"It needs to be in the global scope". Already noticed that, that's exactly what I needed help with...

It didn't work when moved out of OnPluginStart and declaring it inside the Timer_AddPlaytime doesn't look like a very good idea, or is it ?
__________________
Never argue with an idiot. He lowers you to his level and then wins on experience!

Last edited by rtk; 05-17-2014 at 22:07.
rtk is offline
bladez
New Member
Join Date: Mar 2011
Old 05-17-2014 , 23:50   Re: Simple SQL query to update points
Reply With Quote #6

Declare it at the same level that you declared IntervalTime and NumPlayTime. Define it in OnPluginStart.

i.e new Handle:hDatabase = INVALID_HANDLE; and then hDatabase = SQL_TConnect.... Make sure you check that the handle is (still) valid in your timer.
bladez 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 03:03.


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