Raised This Month: $7 Target: $400
 1% 

Mysql Save data


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
IAmReallyCool
Member
Join Date: Nov 2013
Old 11-20-2013 , 06:45   Mysql Save data
Reply With Quote #1

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 ?

Last edited by IAmReallyCool; 11-20-2013 at 06:51.
IAmReallyCool is offline
isotonic
AlliedModders Donor
Join Date: Jun 2011
Location: Moscow, Russia
Old 11-20-2013 , 15:23   Re: Mysql Save data
Reply With Quote #2

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 "
__________________

Last edited by isotonic; 11-20-2013 at 15:23.
isotonic is offline
IAmReallyCool
Member
Join Date: Nov 2013
Old 11-20-2013 , 16:14   Re: Mysql Save data
Reply With Quote #3

No error logs.. Actually these things are all done pretty good in my case. Any other ideas why this happens?
IAmReallyCool is offline
HLM
Senior Member
Join Date: Apr 2008
Location: C:\WINDOWS\System32
Old 11-21-2013 , 14:58   Re: Mysql Save data
Reply With Quote #4

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?
__________________
+|- KARMA Respectively


Last edited by HLM; 11-21-2013 at 14:59.
HLM is offline
Reply


Thread Tools
Display Modes

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 06:56.


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