Veteran Member
|
08-23-2019
, 03:35
Re: Random mysql connection prob
|
#5
|
Thanks Natsheh, i changed that, unfortunately no changes...code is this:
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#include <geoip>
#include <colorchat>
#define PLUGIN "1241"
#define VERSION "1241"
#define AUTHOR "12421"
new Host[] = "localhost"
new User[] = "root"
new Pass[] = ""
new Db[] = "db"
new Handle:g_SqlTuple
new g_Error[512]
new Handle:SqlConnection
new g_iPlayerID[ 33 ]
new bool:g_bLoaded[ 33 ]
new host, user, pass, db
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
set_task( 0.2, "MySql_Init" )
host = register_cvar( "host", "" )
user = register_cvar( "user", "" )
pass = register_cvar( "pass", "" )
db = register_cvar( "db", "" )
get_mapname( g_szMap, charsmax( g_szMap ) )
}
public MySql_Init()
{
new cvar_host[ 50 ], cvar_user[ 50 ], cvar_pass[ 50 ], cvar_db[ 50 ]
get_pcvar_string( host, cvar_host, charsmax( cvar_host ) )
get_pcvar_string( user, cvar_user, charsmax( cvar_user ) )
get_pcvar_string( pass, cvar_pass, charsmax( cvar_pass ) )
get_pcvar_string( db, cvar_db, charsmax( cvar_db ) )
//g_SqlTuple = SQL_MakeDbTuple( cvar_host, cvar_user, cvar_pass, cvar_db )
g_SqlTuple = SQL_MakeDbTuple( "", "", "", "" )
new ErrorCode
SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
set_fail_state(g_Error)
new Handle:Queries
Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS \
BLABLA \
)");
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
SQL_FreeHandle(Queries)
Queries = SQL_PrepareQuery(SqlConnection, "CREATE TABLE IF NOT EXISTS \
BLABLA \
)");
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
SQL_FreeHandle(Queries)
Queries = SQL_PrepareQuery(SqlConnection, "CREATE TABLE IF NOT EXISTS \
BLABLA \
)");
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
SQL_FreeHandle(Queries)
SQL_FreeHandle(SqlConnection)
set_task( 0.3, "handleThatMap" )
}
public handleThatMap()
{
new gszQuery[ 512 ]
formatex(gszQuery, charsmax(gszQuery),"SELECT mid FROM `maps` WHERE mapname = '%s '", g_szMap )
SQL_ThreadQuery(g_SqlTuple, "handleSelectMap", gszQuery);
}
public plugin_end()
{
SQL_FreeHandle( SqlConnection )
}
public handleSelectMap(failstate, Handle:Query, error[], errnum, data[], size)
{
if(failstate != TQUERY_SUCCESS){
log_amx("SQL Insert error: %s",error);
return;
}
if(SQL_NumRows(Query) < 1)
{
new gszQuery[ 512 ]
formatex(gszQuery, charsmax(gszQuery), "INSERT INTO `primekz_maps` VALUES ( '0', '%s' )" , g_szMap );
SQL_ThreadQuery(g_SqlTuple, "IgnoreHandle", gszQuery);
//more correct results than sql_getinsertid
set_task( 0.2, "handleThatMap" )
}
else
{
g_iMID = SQL_ReadResult( Query, 0 )
}
}
public client_putinserver( id )
{
if( !is_user_bot( id ) )
{
set_task( 0.8, "Load_MySql", id )
}
}
public Load_MySql(id)
{
new szSteamId[32], szTemp[512]
get_user_authid(id, szSteamId, charsmax(szSteamId))
new Data[1]
Data[0] = id
format(szTemp,charsmax(szTemp),"SELECT * FROM players WHERE steamid = '%s'", szSteamId)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
{
log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
}
else if(FailState == TQUERY_QUERY_FAILED)
{
log_amx("Load Query failed. [%d] %s", Errcode, Error)
}
new id
id = Data[0]
if(SQL_NumResults(Query) < 1)
{
new szSteamId[32]
get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
if (equal(szSteamId,"ID_PENDING") || containi( szSteamId, "BOT" ) != -1 )
return PLUGIN_HANDLED
new szTemp[ 512 ]
new g_ip[ 15 ], g_country[ 40 ]
new name[ 64 ]
get_user_name( id, name, charsmax( name ) )
SQL_PrepareString( name, name, charsmax( name ));
get_user_ip( id, g_ip, charsmax( g_ip ), 1 )
geoip_country_ex(g_ip, g_country, charsmax( g_country ), -1)
new szCode[ 5 ]
getNationality( id, g_ip, szCode);
format(szTemp,charsmax(szTemp),"INSERT INTO players BLABLABLBLA", blablabla )
SQL_ThreadQuery( g_SqlTuple, "IgnoreHandle", szTemp )
formatex( g_playerCountry[ id ], charsmax( g_playerCountry[] ), g_country )
//sql_getinsertID - never use again:
set_task( 0.4, "Load_MySql", id )
}
else
{
//player loaded
BLABLA...
}
g_bLoaded[ id ] = true
return PLUGIN_HANDLED
}
getNationality(id, const szIP[], szCode[5]){
new szTemp[3];
if(geoip_code2_ex( szIP, szTemp)){
copy(szCode, 4, szTemp);
}
else{
get_user_info(id, "lang", szCode, 2);
SQL_PrepareString(szCode, szCode, 4);
}
}
stock SQL_PrepareString(const szQuery[], szOutPut[], size)
{
copy(szOutPut, size, szQuery);
replace_all(szOutPut, size, "'", "\'");
replace_all(szOutPut,size, "`", "\`");
replace_all(szOutPut,size, "\\", "\\\\");
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}
im skipping cvars so adding directly connect info in:
g_SqlTuple = SQL_MakeDbTuple( "", "", "", "" )
all that happens before load_mysql being called (also handleThatMap)...so mysql_init...
im still confused how it worked first 3-4 days without errors, but now it got stuck for more than 12 hours..im starting to think that cs cant go with mysql, to work 100%..i hope im not right with that opinion...ps: queries are called only on mapstart, player connect, and when player finish the map (~every minute) so not so much going on to the mysql
__________________
|
|