Raised This Month: $85 Target: $400
 21% 

[ISSUE] Error getting data from the database


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SpirT
Senior Member
Join Date: Sep 2018
Location: CreateMyGF(int patience)
Old 05-11-2020 , 07:12   [ISSUE] Error getting data from the database
Reply With Quote #1

Hey there.

I am updating my vipmenu plugin by using an in-build database system instead of using store by zephyrus plugin.

My credits value is stored on the database correctly but when I use the command to get the information from the database and print it to chat it does not match.

PHP Code:
#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "SpirT"
#define PLUGIN_VERSION "1.0"

Database DB null;
char Error[256];

ConVar g_credits;

int GlobalClient[MAXPLAYERS 1];

ConVar g_akprice;
ConVar g_m4price;
ConVar g_m1price;
ConVar g_awpprice;

ConVar g_timer;

ConVar g_usage;

int akprice;
int m4price;
int m1price;
int awpprice;

int usage[MAXPLAYERS 1];
int max;

char cvarCredits[20];

int TimerFloat;

int balance[MAXPLAYERS 1];

Handle CreditTimer[MAXPLAYERS 1];

#include <sourcemod>
#include <sdktools>

#pragma newdecls required

public Plugin myinfo =
{
    
name "[SpirT] VipMenu Credits System DATABASE",
    
author PLUGIN_AUTHOR,
    
description "",
    
version PLUGIN_VERSION,
    
url ""
};

public 
void OnPluginStart()
{
    
DB SQL_Connect("testeconfig"trueErrorsizeof(Error));
    
    if(
DB == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Error conecting to MySQL Database: %s"Error);
        
CloseHandle(DB);
    }
    else
    {
        
PrintToServer("[SpirT - VIPMENU] Connection to MySQL Database succeed.");
        
        
CreateDatabaseTables();
    }
    
    
g_credits CreateConVar("spirt_vipmenu_credits""5""Amount of credits to receive at the end of the interval.");
    
GetConVarString(g_creditscvarCreditssizeof(cvarCredits));
    
    
g_timer CreateConVar("spirt_vipmenu_timer_interval""10""Interval in seconds to give credits to all Players.");
    
TimerFloat GetConVarInt(g_timer);
    
    
g_akprice CreateConVar("spirt_vipmenu_akprice""1000""Credit amount needed to buy this pack");
    
g_m4price CreateConVar("spirt_vipmenu_m4price""1000""Credit amount needed to buy this pack");
    
g_m1price CreateConVar("spirt_vipmenu_m1price""1000""Credit amount needed to buy this pack");
    
g_awpprice CreateConVar("spirt_vipmenu_awpprice""2000""Credit amount needed to buy this pack");
    
akprice GetConVarInt(g_akprice);
    
m4price GetConVarInt(g_m4price);
    
m1price GetConVarInt(g_m1price);
    
awpprice GetConVarInt(g_awpprice);
    
    
g_usage CreateConVar("spirt_vipmenu_maxusage""3""Max amount of uses of the VIPMENU each round.");
    
max GetConVarInt(g_usage);
    
    
RegAdminCmd("sm_scredits"Command_SCreditsADMFLAG_RESERVATION);
    
RegAdminCmd("sm_vipmenu"Command_VipMenuADMFLAG_RESERVATION);
    
    
AutoExecConfig(true"spirt.credits.vipmenu");
}

public 
Action Command_SCredits(int clientint args)
{
    if(
DB == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Connection do MySQL Database failed. This command is temporately disabled.");
        return 
Plugin_Handled;
    }
    
    
char steamid[32];
    
GetClientAuthId(clientAuthId_Steam2steamidsizeof(steamid));
    
    
char squery[256];
    
Format(squerysizeof(squery), "SELECT `credits` FROM spirt_credits WHERE steamid = '%s'"steamid);
    
DBResultSet result SQL_Query(DBsquery);
    if(
result == null)
    {
        
PrintToServer("[SpirT - VIPMENU] An error occured while giving you your credits.");
        return 
Plugin_Handled;
    }
    
    if(
result.FetchRow())
    {
        
PrintToChat(client" \x0B[SpirT - VIPMENU]\x07 You have \x03%d \x04credits"balance[client]);
        return 
Plugin_Handled;
    }
    
    return 
Plugin_Handled;
}

void CreateDatabaseTables()
{
    
char squery[256];
    
Format(squerysizeof(squery), "CREATE TABLE IF NOT EXISTS spirt_credits (steamid varchar(32) NOT NULL, credits int(11) NOT NULL)");
    
DBResultSet query SQL_Query(DBsquery);
    
    if(
query == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Failed to create spirt_credits table.");
        return;
    }
    
    
PrintToServer("[SpirT - VIPMENU] Table spirt_credits created with success.");
    return;
}

public 
void OnClientPutInServer(int client)
{
    if(
DB == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Could not connect to database. Player credits are not being fetched.");
        return;
    }
    
    
GlobalClient[client] = client;
    
    
CreditTimer[client] = CreateTimer(TimerFloat 1.0Timer_InsertDatabaseclientTIMER_REPEAT);
    
char steamid[32];
    
GetClientAuthId(clientAuthId_Steam2steamidsizeof(steamid));
    
    
char selQuery[256];
    
Format(selQuerysizeof(selQuery), "SELECT steamid FROM spirt_credits");
    
DBResultSet select SQL_Query(DBselQuery);
    if(
select == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Could not select player from database.");
        return;
    }
    
    if(
select.FetchRow())
    {
        
balance[client] = SQL_FetchInt(select0);
        return;
    }
    
    
char squery[256];
    
Format(squerysizeof(squery), "INSERT INTO spirt_credits (steamid, credits) VALUES ('%s', 0)"steamid);
    
DBResultSet insert SQL_Query(DBsquery);
    if(
insert == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Could not add a new player to database.");
        return;
    }
    
    
PrintToServer("[SpirT - VIPMENU] A new player called %N was added to the Database."client);
    return;
}

public 
void OnClientDisconnect(int client)
{
    if(
CreditTimer[client] != INVALID_HANDLE)
    {
        
KillTimer(CreditTimer[client]);
        return;
    }
}

public 
Action Timer_InsertDatabase(Handle timerany client)
{
    if(!
IsClientInGame(client))
    {
        return 
Plugin_Continue;
    }
    
    
char squery[256];
    
Format(squerysizeof(squery), "UPDATE spirt_credits SET credits = credits + %s"cvarCredits);
    
DBResultSet query SQL_Query(DBsquery);
    if(
query == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Could not give more %s credits to the client"cvarCredits);
        return 
Plugin_Handled;
    }
    
    
PrintToChat(client" \x0B[SpirT - VIPMENU]\x07 You have received \x03%s \x04credits"cvarCredits);
    return 
Plugin_Handled;
}

public 
Action Command_VipMenu(int clientint args)
{
    if(
usage[client] < max)
    {
        
Main().Display(clientMENU_TIME_FOREVER);
        return 
Plugin_Handled;
    }
    
    
PrintToChat(client" \x0B[SpirT - VIPMENU]\x07 You have reached the \x02max amount of \x04uses per round! \x03Please wait to the next round.");
    return 
Plugin_Handled;
}

Menu Main()
{
    
Menu menu = new Menu(MainHandleMENU_ACTIONS_ALL);
    
menu.SetTitle("Choose a type of Items:");
    
menu.AddItem("1""Guns");
    
menu.AddItem("2""Buffs");
    
menu.ExitButton true;
    
    return 
menu;
}

public 
int MainHandle(Menu menuMenuAction actionint clientint item)
{
    
char choice[32];
    
menu.GetItem(itemchoicesizeof(choice));
    if(
action == MenuAction_Select)
    {
        if(
StrEqual(choice"1"))
        {
            
delete menu;
            
Guns(client).Display(clientMENU_TIME_FOREVER);
        }
        else if(
StrEqual(choice"2"))
        {
            
delete menu;
            
//Buffs(client).Display(client, MENU_TIME_FOREVER);
        
}
    }
    else if(
action == MenuAction_End)
    {
        
delete menu;
    }
}

Menu Guns(int client)
{
    
Menu menu = new Menu(GunsHandleMENU_ACTIONS_ALL);
    
menu.SetTitle("Choose a Gun Pack:");
    
char text[64];
    
Format(textsizeof(text), "Credits: %d"balance[client]);
    
menu.AddItem(texttextITEMDRAW_RAWLINE);
    
    
char aktext[256];
    
char m4text[256];
    
char m1text[256];
    
char awptext[256];
    
Format(aktextsizeof(aktext), "AK47 + DEAGLE [%d]"akprice);
    
Format(m4textsizeof(m4text), "M4A4 + DEAGLE [%d]"m4price);
    
Format(m1textsizeof(m1text), "M4A1-S + DEAGLE [%d]"m1price);
    
Format(awptextsizeof(awptext), "AWP + DEAGLE [%d]"awpprice);
    
    if(
balance[client] < akprice)
    {
        
menu.AddItem(aktextaktextITEMDRAW_RAWLINE);
    }
    
    if(
balance[client] < m4price)
    {
        
menu.AddItem(m4textm4textITEMDRAW_RAWLINE);
    }
    
    if(
balance[client] < m1price)
    {
        
menu.AddItem(m1textm1textITEMDRAW_RAWLINE);
    }
    
    if(
balance[client] < awpprice)
    {
        
menu.AddItem(awptextawptextITEMDRAW_RAWLINE);
    }
    
    
menu.AddItem("1"aktext);
    
menu.AddItem("2"m4text);
    
menu.AddItem("3"m1text);
    
menu.AddItem("4"awptext);
    
menu.ExitBackButton true;
    
menu.ExitButton true;
    
    return 
menu;
}

public 
int GunsHandle(Menu menuMenuAction actionint clientint item)
{
    
char steamid[32];
    
GetClientAuthId(GlobalClient[client], AuthId_Steam2steamidsizeof(steamid));
    
char choice[32];
    
menu.GetItem(itemchoicesizeof(choice));
    if(
action == MenuAction_Select)
    {
        if(
StrEqual(choice"1"))
        {
            
delete menu;
            
usage[client]++;
            
GivePlayerItem(client"weapon_ak47");
            
GivePlayerItem(client"weapon_deagle");
            
UpdateClientCredits(clientakprice);
        }
        else if(
StrEqual(choice"2"))
        {
            
delete menu;
            
//Buffs(client).Display(client, MENU_TIME_FOREVER);
        
}
    }
    else if(
action == MenuAction_End)
    {
        
delete menu;
    }
}

void UpdateClientCredits(int clientint credits)
{
    
char steamid[64];
    
GetClientAuthId(clientAuthId_Enginesteamidsizeof(steamid));
    
char QueryRemoveCredits[256];
    
Format(QueryRemoveCreditssizeof(QueryRemoveCredits), "UPDATE spirt_credits SET credits = credits + %d WHERE steamid = '%s'"creditssteamid);
    
DBResultSet remove SQL_Query(DBQueryRemoveCredits);
    
    if(
DB == null || remove == null)
    {
        
PrintToServer("[SpirT - VIPMENU] Failed to update player credits.");
    }
    else
    {
        
PrintToChat(client" \x0B[SpirT - VIPMENU]\x07 You received: \x03AK47 + DEAGLE");
    }

Is there any issue? As well I need another thing. I need to get the steamID of the client when he clicks in any item of the Menu to remove a credit amount from that client but it prints to console that client index is invalid (0).

How to fix it?

Best Regards,

SpirT.
__________________
SpirT 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 17:17.


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