AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Getting data from from MySQL row (https://forums.alliedmods.net/showthread.php?t=49660)

raa 01-10-2007 01:08

Getting data from from MySQL row
 
I know how to read from csstats, but how do you write to them.

what I'm looking to do is make a "purchase kills" feature, but I do not want the kills purchased shown on the scoreboard. Instead I want the kills to be applied to the users stats already within the csstats.dat.

Does anyone know how to do that?

raa 01-11-2007 21:58

Re: Writing to csstats (CS 1.6)
 
ok so maybe writing to csstats.dat isn't the best to go about it.

What about reading directly from the MySQL database that is already storing the info?

this is what I have so far and it isn't working at all. Please forgive any obvious mistakes, I was trying anything.

Code:

public read_user_stats(id){
        //if(get_cvar_num("sv_ranks")==0)
        //return PLUGIN_HANDLED
       
        //log_amx("[sqlStats] save_user_stats")
        log_amx("[sqlStats] Reading %i Players", g_iMutationcount)
       
        // sql connect
        g_dbc = dbi_connect(g_host,g_user,g_pass,g_dbname,g_error,MAX_NAME_LENGTH)
       
        if (g_dbc == SQL_FAILED)
                log_amx("[sqlStats] SQL Connection Failed")
        else{
                // process all queries
                result = dbi_query(g_dbc,"SELECT * FROM statstable WHERE kills ='%s'")
                new user_kills[32]
                for (new i=1;i<=dbi_num_rows(result);i++) {
                        dbi_nextrow(result)
                        dbi_result(result,"kills",user_kills[id])
                        console_print(id,"Value: %s",user_kills)
                       
                }
                dbi_free_result(result)
                return PLUGIN_HANDLED
        }
        // sql close       
        dbi_close(g_dbc)
       
        //reset mutationcount
        g_iMutationcount = 0
        return 0
}

Table is "statstable", Database row is "kills".

raa 01-12-2007 00:56

Re: Getting data from from MySQL row
 
Ok I got the function to read the rows. However there was a problem. Here's the code, but it displays everyones stats within the database rather then just the person making the query.

Code:

public read_user_stats(id){
        // sql connect
        g_dbc = dbi_connect(g_host,g_user,g_pass,g_dbname,g_error,MAX_NAME_LENGTH)
       
        if (g_dbc == SQL_FAILED)
                log_amx("[sqlStats] SQL Connection Failed")
        else{
                // run a query
                result = dbi_query(g_dbc,"SELECT * FROM statstable WHERE kills AND headshots")
               
                new Data_kills
                new Data_HS
                // notice here that we run the dbi_nextrow first - one of the main reasons I don't like DBI.
                // you MUST do this, it doesn't start at the first row
                while(dbi_nextrow(result))
                        {
                        // get the row
                        // note that we could also use dbi_result(Result,"you")
                        Data_kills = dbi_result(result,"kills")
                        Data_HS = dbi_result(result,"headshots")
                        // tell the server console we have found data
                        client_print(id,print_chat, "* %d kills, %d Headshots.", Data_kills, Data_HS);
                        dbi_close(g_dbc)
                }
        }
}

So... I tried this.... thinking that only the the rows that match the persons authid would be returned. This returns "0" though..

Code:

ublic read_user_stats(id){
        new g_Data_kills[33]
        new g_Data_HS[33]
        new g_authid[32]
        get_user_authid(id,g_authid,31)
       
        // sql connect
        g_dbc = dbi_connect(g_host,g_user,g_pass,g_dbname,g_error,MAX_NAME_LENGTH)
       
        if (g_dbc == SQL_FAILED)
                log_amx("[sqlStats] SQL Connection Failed")
        else{
                // run a query
                result = dbi_query(g_dbc,"SELECT * FROM statstable WHERE authid = '%s'",g_authid)
               
                if (result == RESULT_FAILED) {
                        log_amx("[MySQL Test] MySQL Query Failed!!") // If it failed, we can't get the data.
                        return PLUGIN_CONTINUE // Query failed! Sorry :P
                }
                else if (result == RESULT_NONE) { // If it didn't fail, but they don't have an entry,
                        g_Data_kills[id] = 0 // This will create a new entry in the database
                        g_Data_HS[id] = 0
                }
                else {
                        dbi_nextrow(result) // This retrieves data from your query.
                        g_Data_kills[id] = dbi_result(result,"kills")
                        g_Data_HS[id] = dbi_result(result, "headshots")
                }
                client_print(id,print_chat, "* %d kills, %d Headshots.", g_Data_kills, g_Data_HS);
                dbi_free_result(result) // You MUST free the result! Or else memory leaks will ensue!!!
                dbi_close(g_dbc)
        }
        return PLUGIN_HANDLED;
}


any ideas?

raa 01-12-2007 01:50

Re: Getting data from from MySQL row
 
Got it..

Code:

public read_user_stats(id){
        new query[256]
        new g_authid[32]
        get_user_authid(id,g_authid,31)
       
        // sql connect
        g_dbc = dbi_connect(g_host,g_user,g_pass,g_dbname,g_error,MAX_NAME_LENGTH)
       
        if (g_dbc == SQL_FAILED)
                log_amx("[sqlStats] SQL Connection Failed")
        else{
                // run a query
                format( query, 255, "SELECT authid,kills,headshots FROM statstable WHERE authid='%s'", g_authid)
                result = dbi_query(g_dbc, query)
               
                if (g_dbc == SQL_FAILED )
                        {
                        server_print( "Query failed!")
                        dbi_free_result(result)
                        return PLUGIN_HANDLED
                }
                new g_Data_kills
                new g_Data_HS
                while(dbi_nextrow(result))
                        {
                        // get the row
                        // note that we could also use dbi_result(Result,"you")
                        g_Data_kills = dbi_result(result,"kills")
                        g_Data_HS = dbi_result(result,"headshots")
                        // tell the server console we have found data
                        client_print(id,print_chat, "* %d kills, %d Headshots.", g_Data_kills, g_Data_HS);
                        dbi_close(g_dbc)
                }
        }
        return PLUGIN_HANDLED
}



All times are GMT -4. The time now is 22:30.

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