AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Mysql Save data (https://forums.alliedmods.net/showthread.php?t=230118)

IAmReallyCool 11-20-2013 06:45

Mysql Save data
 
So I made this :

PHP Code:

// Includes
#include <amxmodx>
#include <cstrike>
#include <amxmisc>
#include <sqlx>

// Pragma
#pragma semicolon 1

// Plugin info
new const PLUGIN[] =    "MT Pts";
new const 
VERSION[] =    "1.0";
new const 
AUTHOR[] =    "Cooler";

// MySQL Init ---------------------------------
new Host[]        = "";
new 
User[]        = "";
new 
Pass[]        = "";
new 
Db[]       = "";
new 
TABLE[] = "";
// ---------------------------------------------------

// Bool
new g_Points[33];
new 
Handle:g_SqlConnection;
new 
Handle:g_SqlTuple;
new 
g_Error[512];
new 
g_sql_ready false;
new 
bool:g_loaded[33];
new 
maxplayers;

public 
plugin_init()
{
    
// Register the plugin
    
register_plugin(PLUGINVERSIONAUTHOR);
    
    
maxplayers get_maxplayers();
    
    
set_task(0.1"MySql_Init");
}

public 
MySql_Init()
{
    
g_SqlTuple SQL_MakeDbTuple(Host,User,Pass,Db);
    
    new 
ErrorCode;
    
g_SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error));
    if(
g_SqlConnection == Empty_Handle)
    {
        
set_fail_state(g_Error);
    }
    
    new 
Handle:Queries;
    
Queries SQL_PrepareQuery(g_SqlConnection,"CREATE TABLE IF NOT EXISTS %s (name varchar(64), points INT(11))"TABLE);
    
    if(!
SQL_Execute(Queries))
    {
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error));
        
set_fail_state(g_Error);
    }
    
    
SQL_FreeHandle(Queries);
    
    
g_sql_ready true;
    for(new 
i=1i<=maxplayersi++)
    {
        
Load_MySql(i);
    }
}

public 
Load_MySql(id)
{
    if(
g_sql_ready)
    {
        if(
g_SqlTuple == Empty_Handle)
        {
            
set_fail_state(g_Error);
        }
        
        new 
szName[32], szTemp[512];
        
get_user_name(idszNamecharsmax(szName));
        
        new 
Data[1];
        
Data[0] = id;
        
        
format(szTemp,charsmax(szTemp),"SELECT * FROM `%s` WHERE (`%s`.`name` = '%s')"TABLETABLEszName);
        
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"ErrcodeError);
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError);
    }
    new 
id;
    
id Data[0];
    if(
SQL_NumResults(Query) < 1)
    {
        
//.if there are no results found
        
        
new szName[32], szQuotedName[64];
        
get_user_name(idszName31); // get user's name
        
SQL_QuoteString(g_SqlConnectionszQuotedName63szName);
        
        new 
szTemp[512];
        
        
// now we will insert the values into our table.
        
format(szTemp,charsmax(szTemp),"INSERT INTO `%s` ( `name` , `points`) VALUES ('%s','0');"TABLEszQuotedName);
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp);
    }
    else
    {
        
// if there are results found
        
g_Points[id]        = SQL_ReadResult(Query2);
    }
    
g_loaded[id] = true;
    return 
PLUGIN_HANDLED;
}

public 
Save_MySql(id)
{
    if(
g_loaded[id])
    {
        new 
szTemp[512], szName[32], szQuotedName[64];
        
get_user_name(idszName31);
        
SQL_QuoteString(g_SqlConnectionszQuotedName63szName);
        
        
// Here we will update the user hes information in the database where the steamid matches.
        
format(szTemp,charsmax(szTemp),"UPDATE `%s` SET `name` = '%s', `points` = '%i'",TABLEszQuotedName,g_Points[id]);
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp);
    }
}

public 
IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
SQL_FreeHandle(Query);
    
    return 
PLUGIN_HANDLED;
}

// When client enter on server
public client_putinserver(id)
{
    if (!
is_user_bot(id))
    {
        
// Set the custom HUD display task
        
set_task(1.0"ShowHud"id+TASK_SHOWHUD__"b");
    }
    
    
Load_MySql(id);
}

// When client disconnect
public client_disconnect(id)
{
    
Save_MySql(id);
    
remove_task(id+TASK_SHOWHUD);
}

public 
plugin_end()
{
    if(
g_SqlConnection != Empty_Handle)
    {
        
SQL_FreeHandle(g_SqlConnection);
    }


Its not even creating a database... and it doesnt save my points, what shall I do ?

isotonic 11-20-2013 15:23

Re: Mysql Save data
 
Any error logs?

Consider revising the SQL queries:
- back quotes (`) are used only for column names in rare cases
- you don't need use quotes (' and ") with numbers
- you must quote strings with ' or "

IAmReallyCool 11-20-2013 16:14

Re: Mysql Save data
 
No error logs.. Actually these things are all done pretty good in my case. Any other ideas why this happens?

HLM 11-21-2013 14:58

Re: Mysql Save data
 
PHP Code:

Queries SQL_PrepareQuery(g_SqlConnection,"CREATE TABLE IF NOT EXISTS %s (name varchar(64), points INT(11))"TABLE); 

I have always had problems getting SQL_PrepareQuery to use formatting properly, so I just made the query elsewhere in a string variable formatted properly and passed the variable as the query

PHP Code:

new Table[82];//64(-2) characters before table name
format(Tablecharsmax(Table), "CREATE TABLE IF NOT EXISTS %s (name varchar(64), points INT(11))"TABLE);
Queries SQL_PrepareQuery(g_SqlConnectionTable); 

have you tried to manually create the table and then see if the rest of your code executes?


All times are GMT -4. The time now is 23:15.

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