Raised This Month: $51 Target: $400
 12% 

MySQL Setup


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DJ Data
SourceMod Donor
Join Date: Dec 2012
Location: Switzerland
Old 09-02-2015 , 09:53   MySQL Setup
Reply With Quote #1

Hey,

I've been trying to figure out how to setup a full sql script all morning, I can't get my head around it.
https://wiki.alliedmods.net/SQL_(SourceMod_Scripting) tells you how to format queries and such, but i dont know how to use the actual value that i get from SELECT.

What im trying to do:
I want to show a number (points) in a menu item. Whenever the menu is opened, it should make a call. However, i heard this can cause lag spikes and that would make this unusable. I've heard about sql threading but i honestly don't understand a single bit of it.

I'd be thankful if someone could show me a script to fulfill what i've described above.

Cheers!
__________________
SourcePawn Coding Level: Novice
DJ Data is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 09-02-2015 , 09:57   Re: MySQL Setup
Reply With Quote #2

Quote:
Originally Posted by DJ Data View Post
i dont know how to use the actual value that i get from SELECT.
The "processing results" section has information about this
Miu is offline
DJ Data
SourceMod Donor
Join Date: Dec 2012
Location: Switzerland
Old 09-02-2015 , 10:09   Re: MySQL Setup
Reply With Quote #3

Quote:
Originally Posted by Miu View Post
The "processing results" section has information about this
Looking at that confuses me so much haha, i wish there were some YouTube videos with indepth examples for us noobs ^^
__________________
SourcePawn Coding Level: Novice
DJ Data is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-02-2015 , 10:26   Re: MySQL Setup
Reply With Quote #4

With normal queries, when you send a statement the game server stops and sits there until the database server gives you the results. Hence the lag spikes as the server stops processing everything until it returns.

With threaded queries, you send a statement and a callback for it to call. When the database server has the results, your callback is called. In the meantime, the game server goes on its merry way doing stuff like processing game frames. The main downside here is that you have no idea when the results will arrive.

Then again, if you're using an external DB for time-sensitive information, your code is going to have issues anyway.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 09-02-2015 at 10:26.
Powerlord is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 09-02-2015 , 10:46   Re: MySQL Setup
Reply With Quote #5

PHP Code:
void ShowPoints(int client, const char[] steamid)
{
    
char query[128];
    
FormatEx(querysizeof(query), "SELECT points FROM table WHERE steamid = '%s'"steamid);
    
SQL_TQuery(g_hDBcallbackqueryclient);
}

public 
void callback(Handle:ownerHandle:hndl, const String:error[], any:client)
{
    if(
hndl == INVALID_HANDLE)
    {
        
LogError("error: %s"error);
        return;
    }
    
    
SQL_FetchRow(hndl);
    
    
int points SQL_FetchInt(hndl0);
    
    
PrintToChat(client"You have %d points!!!!!!!!!!!"points);


Last edited by Miu; 09-02-2015 at 11:03.
Miu is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-02-2015 , 11:51   Re: MySQL Setup
Reply With Quote #6

Pass the menu handle through the optional callback parameter so you can use it on the callback.
Potato Uno is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 09-02-2015 , 12:05   Re: MySQL Setup
Reply With Quote #7

@Miu
Don't use client indexes with asynchronous functions, use an userid or serial instead. You should also check that a row was fetched before trying to retrieve data.
__________________

Last edited by Impact123; 09-02-2015 at 12:07.
Impact123 is offline
DJ Data
SourceMod Donor
Join Date: Dec 2012
Location: Switzerland
Old 09-04-2015 , 08:19   Re: MySQL Setup
Reply With Quote #8

PHP Code:
AddMenuItem(menu"tokens""You have %d VIP Tokens"tokens);

stock ShowPoints(clientString:steamid[])
{
    new 
String:query[128];
    
FormatEx(querysizeof(query), "SELECT points FROM Users WHERE steamid = '%s'"steamid);
    
SQL_TQuery(g_hDBcallbackqueryclient);
}

public 
Action:callback(Handle:ownerHandle:hndl, const String:error[], any:client)
{
    if(
hndl == INVALID_HANDLE)
    {
        
LogError("error: %s"error);
        return;
    }
    
    
SQL_FetchRow(hndl);
    
    
tokens SQL_FetchInt(hndl0);

So this would essentially work? As well as not freeze the server whenever someone opens the menu?
Also, I've converted the 1.7 code back to 1.6 because I dont feel comfortable with 1.7 yet.
__________________
SourcePawn Coding Level: Novice
DJ Data is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 09-04-2015 , 08:38   Re: MySQL Setup
Reply With Quote #9

i don't know where you're creating the menu but probably not, you should create it imn the callback
Miu is offline
DJ Data
SourceMod Donor
Join Date: Dec 2012
Location: Switzerland
Old 09-04-2015 , 08:39   Re: MySQL Setup
Reply With Quote #10

Quote:
Originally Posted by Miu View Post
i don't know where you're creating the menu but probably not, you should create it imn the callback
The menu is called when someone types !menu, if thats what you mean.

Edit:

Here is the full (minimalized) code:
PHP Code:
new Handle:hDatabase INVALID_HANDLE;

public 
OnPluginStart() 

    
//reg command
    
RegAdminCmd("sm_menu"Command_menuADMFLAG_ROOT); 
    
    
//Make Threaded DB Connection
    
StartSQL();
}

//Main Menu 
public Action:Command_vipmenu(clientargs

    new 
Handle:menu CreateMenu(vipmenuMenuAction_Select MenuAction_End); 
    
SetMenuTitle(menu"► arctekservers.com ◄"); 
    
    
AddMenuItem(menu"tokens""You have %d VIP Tokens"tokens);
    
    
DisplayMenu(menuclientMENU_TIME_FOREVER); 


stock ShowPoints(clientString:steamid[])
{
    new 
String:query[128];
    
FormatEx(querysizeof(query), "SELECT points FROM Users WHERE steamid = '%s'"steamid);
    
SQL_TQuery(g_hDBGotDatabasequeryclient);
}

StartSQL()
{
    
SQL_TConnect(GotDatabase);
}

public 
GotDatabase(Handle:ownerHandle:hndl, const String:error[], any:client)
{
    if(
hndl == INVALID_HANDLE)
    {
        
LogError("error: %s"error);
        return;
    }
    
    
SQL_FetchRow(hndl);
    
    
tokens SQL_FetchInt(hndl0);

2 things i dont get:
Where do I define which database the TConnect is using in my databases.cfg?
And when is ShowPoints called?
__________________
SourcePawn Coding Level: Novice

Last edited by DJ Data; 09-04-2015 at 08:58.
DJ Data 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 17:59.


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