AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQLX How to get coluumn with highest number? (https://forums.alliedmods.net/showthread.php?t=299038)

gameplayonline 06-30-2017 12:07

SQLX How to get coluumn with highest number?
 
Please How i can do this:
From mysql get column 1 with row where is saved highest number and save it to local variable
if i have in column 1 10 rows and have saved data 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 i need get 10 if i will add 11 i need get 11...

klippy 06-30-2017 12:32

Re: SQLX How to get coluumn with highest number?
 
Code:

SELECT MAX(ColumnName) FROM DatabaseName;

gameplayonline 07-01-2017 03:47

Re: SQLX How to get coluumn with highest number?
 
I have this but its not working. Its set UID to 0 and if will work this new num = SQL_ReadResult(Query2, 0)
new addUid = num + 1
it must be 1 because default it get 0

Code:

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)
        }
       

                       
        // ok, we're ready to connect
        new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
        if(SqlConnection == Empty_Handle)
        // stop the plugin with an error message
        set_fail_state(g_Error)
       
        new id       
        id = Data[0]
       
        new Handle:Query2
        new Handle:Query3
       
       
       
        if(SQL_NumResults(Query) < 1)
        {
                //.if there are no results found
                new szSteamId[32]
                get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
               
                //  if its still pending we can't do anything with it
                if (equal(szSteamId,"ID_PENDING"))
                        return PLUGIN_HANDLED
               
                new szTemp[512]
               
                // now we will insturt the values into our table.
                format(szTemp,charsmax(szTemp),"INSERT INTO `player` ( `SteamID` , `UID`, `Referrals`, `isReferral`)VALUES ('%s','%d','%d','%d');",szSteamId, uid, referrals, isReferral)
                SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
               
                Query2 = SQL_PrepareQuery(SqlConnection, "SELECT MAX(UID) FROM player")
                SQL_Execute(Query2)
                if(SQL_NumResults(Query2) == 1)
                {
                        get_user_authid( id, steamID, charsmax(steamID))
               
                        //  if its still pending we can't do anything with it
                        if (equal(steamID,"ID_PENDING"))
                                return PLUGIN_HANDLED
                       
                        new num = SQL_ReadResult(Query2, 0)
                        new addUid = num + 1
                        Query3 = SQL_PrepareQuery(SqlConnection, "UPDATE `player` SET `UID` = '%d' WHERE (`player`.`SteamID` = '%s';",addUid, steamID);
                        SQL_Execute(Query3);
                        client_print(id, print_chat, "Uspesne si sa zaregistroval do referral systemu. Napis /uid aby si zistil svoje UID")
                        SQL_FreeHandle(Query2)
                        SQL_FreeHandle(Query3)
                }
                else
                {
                        client_print(id, print_chat, "Chyba")
                }
        }
       
        else
        {
                client_print(id, print_chat, "Uz si bol zaregistrovany, napis /uid pre zistenie UID")
        }       
        return PLUGIN_HANDLED
}


gameplayonline 07-01-2017 22:01

Re: SQLX How to get coluumn with highest number?
 
If it will help somebody i fixed it with this:
Code:

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)
        {
                //.if there are no results found
                new szSteamId[32]
                get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
               
                //  if its still pending we can't do anything with it
                if (equal(szSteamId,"ID_PENDING"))
                        return PLUGIN_HANDLED
               
                new szTemp[512]
               
                // now we will insturt the values into our table.
                format(szTemp,charsmax(szTemp),"INSERT INTO `player` ( `SteamID` , `UID`, `Referrals`, `isReferral`)VALUES ('%s','%d','%d','%d');",szSteamId, uid, referrals, isReferral)
                SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
                addId(id)
        }
       
        else
        {
                client_print(id, print_chat, "Uz si bol zaregistrovany, napis /uid pre zistenie UID")
        }       
        return PLUGIN_CONTINUE


public addId(id)
{
        // READ MAIN DATA
        new Data[1]
        Data[0] = id
                       
        // ok, we're ready to connect
        new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
        if(SqlConnection == Empty_Handle)
                // stop the plugin with an error message
                set_fail_state(g_Error)
       
        new Handle:Query2
       
       
        Query2 = SQL_PrepareQuery(SqlConnection, "SELECT MAX(UID) FROM player")
        SQL_Execute(Query2)
        if(SQL_NumResults(Query2) == 1)
        {
                //.if there are no results found
                new szTemp[512]
                new szSteamId[32]
                get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
               
                //  if its still pending we can't do anything with it
                if (equal(szSteamId,"ID_PENDING"))
                        return PLUGIN_HANDLED
                       
                new num = SQL_ReadResult(Query2, 0)
                client_print(id, print_chat, "num %d", num)
                new addUid = num + 1
                client_print(id, print_chat, "addid +1 = %d", addUid)
                format(szTemp,charsmax(szTemp),"UPDATE `player` SET `UID` = '%d' WHERE `player`.`SteamID` = '%s';", addUid, szSteamId);
                SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
                client_print(id, print_chat, "Uspesne si sa zaregistroval do referral systemu. Napis /uid aby si zistil svoje UID")
               
                       
        }
        else
        {
                client_print(id, print_chat, "Chyba")
        }
        SQL_FreeHandle(Query2)
        // you free everything with SQL_FreeHandle
        SQL_FreeHandle(SqlConnection) 
        return PLUGIN_CONTINUE
}



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

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