View Single Post
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 07-21-2011 , 17:02   Re: How to get date from database ?
Reply With Quote #10

ok, you obviously tried to help yourself.
i like that, so here's some more help:
i quickly wrote this to push you in the right direction. i guess almost everything you need is in there.
this is completely untested and you also need to adapt the db name and stuff. this is just meant as an example which you can heavily copy+paste.
but please, still try to understand what's happening. if you have any further questions, feel free to ask.

PHP Code:
#pragma semicolon 1
#include <sourcemod>

new Handle:g_hDatabase INVALID_HANDLE;
new 
Handle:g_hCvarDBName;

new 
String:g_sDatabaseName[64];

public 
OnPluginStart() {
    
g_hCvarDBName CreateConVar("sm_yourpluginname_dbname""admintools""Use this db for your plugin."FCVAR_PLUGIN);

    
HookConVarChange(g_hCvarDBNameCvar_Changed);

    
RegConsoleCmd("sm_querydb"Command_QueryDB);
    
RegConsoleCmd("sm_querydb_unixtimestamp"Command_QueryDBUnix);
}


public 
Cvar_Changed(Handle:convar, const String:oldValue[], const String:newValue[]) {
    
OnConfigsExecuted();
}

public 
OnConfigsExecuted() {
    
GetConVarString(g_hCvarDBName,g_sDatabaseName,sizeof(g_sDatabaseName));

    
SQL_TConnect(SQL_Connectedg_sDatabaseName);
}

public 
SQL_Connected(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if(
g_hDatabase != INVALID_HANDLE) {
        
CloseHandle(g_hDatabase);
        
g_hDatabase INVALID_HANDLE;
    }

    if (
hndl == INVALID_HANDLE)
    {
        
LogError("Database error: %s"error);
        return;
    }

    
g_hDatabase hndl;
    
LogMessage("Connected successfully to database");
}

public 
Action:Command_QueryDB(client,args) {
    if(
g_hDatabase == INVALID_HANDLE)
        return;

    
decl String:sSteamID[32];
    
GetClientAuthString(clientsSteamIDsizeof(sSteamID));

    
decl String:sQuery[1024];
    
Format(sQuerysizeof(sQuery), "SELECT last_date FROM premium_users_dust2only WHERE steam_id = '%s'"sSteamID);
    
SQL_TQuery(g_hDatabaseSQL_ProcessResultsQueryclientDBPrio_High);
}

public 
SQL_ProcessResult(Handle:dbHandle:hQuery, const String:error[], any:client)
{
    if(
hQuery == INVALID_HANDLE) {
        
LogError("Query failed: %s"error);
        return;
    }

    if(
SQL_FetchRow(hQuery))
    {
        new 
String:sDate[255];
        
SQL_FetchString(hQuery0sDatesizeof(sDate));

        
PrintToChat(client"Expiry date: %s"sDate);
    }

    
CloseHandle(hQuery);
}

public 
Action:Command_QueryDBUnix(client,args) {
    if(
g_hDatabase == INVALID_HANDLE)
        return;

    
decl String:sSteamID[32];
    
GetClientAuthString(clientsSteamIDsizeof(sSteamID));

    
decl String:sQuery[1024];
    
Format(sQuerysizeof(sQuery), "SELECT UNIX_TIMESTAMP(TIMESTAMP(last_date))-UNIX_TIMESTAMP(now()) AS seconds_left FROM premium_users_dust2only WHERE steam_id = '%s'"sSteamID);
    
SQL_TQuery(g_hDatabaseSQL_ProcessResultUnixsQueryclientDBPrio_High);
}

public 
SQL_ProcessResultUnix(Handle:dbHandle:hQuery, const String:error[], any:client)
{
    if(
hQuery == INVALID_HANDLE) {
        
LogError("Query failed: %s"error);
        return;
    }

    if(
SQL_FetchRow(hQuery))
    {
        new 
iSecondsLeft SQL_FetchInt(hQuery0);

        if(
iSecondsLeft 0) {
            
PrintToChat(client"Seconds left: %i"iSecondsLeft);
        }
    }

    
CloseHandle(hQuery);

__________________
einmal mit profis arbeiten. einmal.
Thrawn2 is offline