Raised This Month: $32 Target: $400
 8% 

Mysql print tables! (Need help)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
VIDEN
Member
Join Date: Jul 2008
Old 05-24-2015 , 04:13   Mysql print tables! (Need help)
Reply With Quote #1

Hello! I'm doing an simple rank plugin thats works with my mysql database. I want to print kills deaths and assists (stats from my mysql database) when u write /stats but i cant get it to work.

It had been very nice if someone could help me with it and show me how to do it. (:

Last edited by VIDEN; 05-24-2015 at 04:14.
VIDEN is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 05-24-2015 , 04:51   Re: Mysql print tables! (Need help)
Reply With Quote #2

Code:
#define TABLENAME MyTable
//Need to know table structure, 1st field (column) have 0 number
#define FIELDNUM_STEAMID 0
#define FIELDNUM_KILLS 1
#define FIELDNUM_ASSISTS 2
#define FIELDNUM_DEATHS 3

//somewhere in code (In cmd handler)
char query[255], steamid[32];
GetClientAuthId(client, AuthId_Steam2, steamid, 32);
Format(query, 255, "SELECT * FROM `%s` WHERE `steamid` LIKE '%s';", TABLENAME, steamid);
g_db.Query(T_ReadPlayerStats, query, client);
//

//handler function
public void T_ReadPlayerStats(Database db, DBResultSet results, const char[] error, any client)
{
  if (results == INVALID_HANDLE)
    LogError("Failed to query (error: %s)", error);
  else {
    results.FetchRow();
    //Getting results
    int kills = results.FetchInt(FIELDNUM_KILLS);
    int assists = results.FetchInt(FIELDNUM_ASSISTS);
    int deaths = results.FetchInt(FIELDNUM_DEATHS);
    PrintToChat(client, "Your stats: %d kills, %d assists, %d deaths.", kills, assists, deaths);
    //
  }
}

Last edited by Kailo; 05-24-2015 at 04:57.
Kailo is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 05-24-2015 , 08:09   Re: Mysql print tables! (Need help)
Reply With Quote #3

Here are a few suggestions
  • Don't use a like query when it's not needed
  • Don't select all fields with *, only the ones you need
  • Don't try to fetch fields without checking if there were any rows in the result set first
  • Use sizeof instead of writing the size of strings / arrays twice
  • Don't pass client indexes to asynchronous functions
__________________

Last edited by Impact123; 05-24-2015 at 08:18.
Impact123 is offline
VIDEN
Member
Join Date: Jul 2008
Old 05-24-2015 , 14:59   Re: Mysql print tables! (Need help)
Reply With Quote #4

Thx you both! I wil try it! ( :
VIDEN is offline
VIDEN
Member
Join Date: Jul 2008
Old 05-25-2015 , 12:57   Re: Mysql print tables! (Need help)
Reply With Quote #5

When I use your code Kalio I get one error. "error 17: undifined symbol g_db"

How do I define it? Sry for the newbie question.

Last edited by VIDEN; 05-25-2015 at 12:59.
VIDEN is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 05-25-2015 , 14:25   Re: Mysql print tables! (Need help)
Reply With Quote #6

g_db is global variable of Database type. (Prefix g_ mean that this is need to be global)
I do (in my plugins) connection to database in OnPluginStart and delete handle in OnPluginEnd.
Then you connect need to store a connection handle in var. g_db is var where i store this handle.
Example:
Code:
Database g_db;

OnPluginStart()
{
  g_db = SQL_Connect("default", false, error, 255);
  if (g_db == INVALID_HANDLE)
    LogError("Could not connect: %s", error);
  g_db.SetCharset("utf8");
}

OnPluginEnd()
{
  delete g_db;
}
Kailo is offline
VIDEN
Member
Join Date: Jul 2008
Old 05-25-2015 , 15:30   Re: Mysql print tables! (Need help)
Reply With Quote #7

Thank you for that information! I works nice now! (:
VIDEN is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 05-25-2015 , 16:34   Re: Mysql print tables! (Need help)
Reply With Quote #8

A few more suggestions
  • Plugins can be late-loaded during the game - You should use SQL_TConnect to not block the server while the connection is being established
  • Database handles don't need to be closed manually when the plugin is being unloaded
  • Instead of using LogError, use SetFailState - In case the handle is invalid, your plugin would log your error message and then an invalid handle error would be thrown because you're still passing it to SetCharSet
__________________

Last edited by Impact123; 05-25-2015 at 16:50.
Impact123 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 16:45.


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