Raised This Month: $ Target: $400
 0% 

[TF2] Mysql connect


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
superchat
Junior Member
Join Date: Sep 2012
Old 12-26-2012 , 11:04   [TF2] Mysql connect
Reply With Quote #1

Hello! How to make mysql connect? I read this, but no't help me... I make databases.cfg this:

PHP Code:
    "test"
    
{
        
"driver"            "default"
        "host"                "localhost"
        "database"            "test"
        "user"                "root"
        "pass"                "*****"
        "port"                "3306"
    

superchat is offline
TheKiwI
Member
Join Date: May 2011
Old 12-26-2012 , 12:50   Re: [TF2] Mysql connect
Reply With Quote #2

PHP Code:
    "test"
    
{
        
"driver"            "mysql"
        "host"                "127.0.0.1 (localhost) - or your adress"
        "database"            "DB name"
        "user"                "DB user"
        "pass"                "DB password"
        "port"            "3306"
    

__________________

Last edited by TheKiwI; 12-26-2012 at 12:53.
TheKiwI is offline
superchat
Junior Member
Join Date: Sep 2012
Old 12-26-2012 , 18:33   Re: [TF2] Mysql connect
Reply With Quote #3

Quote:
Originally Posted by TheKiwI View Post
PHP Code:
    "test"
    
{
        
"driver"            "mysql"
        "host"                "127.0.0.1 (localhost) - or your adress"
        "database"            "DB name"
        "user"                "DB user"
        "pass"                "DB password"
        "port"            "3306"
    

I need working code connecting MYSQL.
superchat is offline
SEGnosis
Senior Member
Join Date: Oct 2012
Old 12-26-2012 , 18:50   Re: [TF2] Mysql connect
Reply With Quote #4

Quote:
Originally Posted by superchat View Post
I need working code connecting MYSQL.
I cut these parts out of one of my plugins.
It does not have escaping or full error checking so that is something you might want to add.

PHP Code:
new Handle:db;


/* Start up */

public OnPluginStart()
{
    new 
String:error[255];
    
db SQL_Connect("test"falseerrorsizeof(error));
    
    if (
db == INVALID_HANDLE)
        
LogError(error);


    
HookEvent("player_death"Event_PlayerDeath);
}


/* SQL */

public SQL_SlowCommand(String:command[], any:...)
{
    new 
Handle:result INVALID_HANDLE;
    
    if (
db != INVALID_HANDLE)
    {
        new 
String:formatted[255];
        
VFormat(formattedsizeof(formatted), command2);
        
result SQL_Query(dbformatted);
    }
        
    return 
result;
}

public 
SQL_FastCommand(String:command[], any:...)
{
    if (
db != INVALID_HANDLE)
    {
        new 
String:formatted[255];
        
VFormat(formattedsizeof(formatted), command2);        
        new 
Handle:result SQL_FastQuery(dbformatted);
    }
}


/* Events */

public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
attackee GetClientOfUserId(GetEventInt(event"userid"));
    new 
attacker GetClientOfUserId(GetEventInt(event"attacker"));
    
    
    if(
attacker && GetClientTeam(attackee) != GetClientTeam(attacker))
    {
        new 
String:auth[32];
        
GetClientAuthString(attackerauthsizeof(auth));
        
SQL_FastCommand("UPDATE users SET points = points + 1 WHERE steam_id='%s' AND points < 100"auth);
    }
}

public 
OnClientPutInServer(client)
{
    new 
String:auth[32];
    
GetClientAuthString(clientauthsizeof(auth));
    
SQL_FastCommand("INSERT INTO users VALUES('%s', UNIX_TIMESTAMP(), 100)"auth);

__________________


Last edited by SEGnosis; 12-26-2012 at 18:55.
SEGnosis is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 12-26-2012 , 22:31   Re: [TF2] Mysql connect
Reply With Quote #5

Quote:
Originally Posted by SEGnosis View Post
I cut these parts out of one of my plugins.
It does not have escaping or full error checking so that is something you might want to add.

PHP Code:
new Handle:db;


/* Start up */

public OnPluginStart()
{
    new 
String:error[255];
    
db SQL_Connect("test"falseerrorsizeof(error));
    
    if (
db == INVALID_HANDLE)
        
LogError(error);


    
HookEvent("player_death"Event_PlayerDeath);
}


/* SQL */

public SQL_SlowCommand(String:command[], any:...)
{
    new 
Handle:result INVALID_HANDLE;
    
    if (
db != INVALID_HANDLE)
    {
        new 
String:formatted[255];
        
VFormat(formattedsizeof(formatted), command2);
        
result SQL_Query(dbformatted);
    }
        
    return 
result;
}

public 
SQL_FastCommand(String:command[], any:...)
{
    if (
db != INVALID_HANDLE)
    {
        new 
String:formatted[255];
        
VFormat(formattedsizeof(formatted), command2);        
        new 
Handle:result SQL_FastQuery(dbformatted);
    }
}


/* Events */

public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
attackee GetClientOfUserId(GetEventInt(event"userid"));
    new 
attacker GetClientOfUserId(GetEventInt(event"attacker"));
    
    
    if(
attacker && GetClientTeam(attackee) != GetClientTeam(attacker))
    {
        new 
String:auth[32];
        
GetClientAuthString(attackerauthsizeof(auth));
        
SQL_FastCommand("UPDATE users SET points = points + 1 WHERE steam_id='%s' AND points < 100"auth);
    }
}

public 
OnClientPutInServer(client)
{
    new 
String:auth[32];
    
GetClientAuthString(clientauthsizeof(auth));
    
SQL_FastCommand("INSERT INTO users VALUES('%s', UNIX_TIMESTAMP(), 100)"auth);

needs to be threaded. or it will lag the server
Mitchell is offline
DHJ
Member
Join Date: Feb 2007
Old 12-27-2012 , 04:05   Re: [TF2] Mysql connect
Reply With Quote #6

^

how do you thread it properly?
DHJ is offline
PriceLess
Senior Member
Join Date: Sep 2012
Location: Jungle
Old 12-27-2012 , 05:54   Re: [TF2] Mysql connect
Reply With Quote #7

threaded connect should look something like this :

PHP Code:
#include <sourcemod>

new Handle:hDatabase INVALID_HANDLE;

public 
OnPluginStart()
{
    if(!
SQL_CheckConfig("databasename"))
    
PrintToServer("failed to connect to database.cfg");
    
SQL_TConnect(Callback"databasename");
}

public 
Callback(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if(
hndl == INVALID_HANDLE)
    {
        
PrintToServer("Failed to query (%s)"error);
    }
    else
    {
        
PrintToServer("Connected");
        
hDatabase hndl;
    }

Hope it helps out!
__________________
sincerely PriceLess

Last edited by PriceLess; 12-27-2012 at 05:57. Reason: typo
PriceLess 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 23:59.


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