AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   loop sql select (https://forums.alliedmods.net/showthread.php?t=325636)

krisztian2 06-30-2020 11:29

loop sql select
 
Hi guys!

I have a problem with sql loop. I want to SUM F0,F1,F2,F3,F4 column values and copy the results to
DB_skins array. So the code is:
PHP Code:

new szQuery[2048];
    new 
len 0;
    new 
szData[2];
    
szData[0] = id;
    
szData[1] = get_user_userid(id);
        
//This is the problem
    
for(new i=0<= 4;i++)
    {
    
len += format(szQuery[len], 2048"SELECT SUM(F%i) as F%i FROM accounts; ",i,i);
}
    
SQL_ThreadQuery(g_SqlTuple,"sql_piece_of_skins_thread"szQueryszData2);

public 
sql_piece_of_skins_thread(FailState,Handle:Query,Error[],Errcode,szData[],DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
    {
        
set_fail_state("[ *ERROR* ] YOU CANNOT CONNECT TO THE DATABASE!");
        return;
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
set_fail_state("[ *ERROR* ] QUERY PROBLEM.");
        return;
    }
    
    if(
Errcode)
    {
        
log_amx("[ *ERROR* ] QUERY ERROR ( %s )",Error);
        return;
    }
    
    new 
id szData[0];
    
    if (
szData[1] != get_user_userid(id))
        return;
    
        for(new 
i=0;<= 4;i++)
        {
        
DB_skins[i][id] = SQL_ReadResult(Queryi);
        }
    
inventory(id);
    


Error is:
Code:

L 06/30/2020 - 17:07:21: [MySQL] Invalid column: 1
L 06/30/2020 - 17:07:21: [AMXX] Displaying debug trace (plugin "plugin.amxx")
L 06/30/2020 - 17:07:21: [AMXX] Run time error 10: native error (native "SQL_ReadResult")


spamipolito 07-01-2020 15:17

Re: loop sql select
 
PHP Code:


#define MAX_SKINS 4

public LoadSkins(const id)
{
    static 
szQuery[256], szData[1]

    new 
iLen 0;

    
iLen += formatexszQueryiLen ], charsmaxszQuery ) - iLen"SELECT ")

    for(new 
i=0<= MAX_SKINS;i++)
        
iLen += formatexszQueryiLen ], charsmaxszQuery ) - iLen"`F%d`%s "i== MAX_SKINS "" ",")

    
iLen += formatexszQueryiLen ], charsmaxszQuery ) - iLen"FROM `accounts` WHERE `PlayerID`='%d'"get_user_authid(id) )

    
szData[0] = id;

    
SQL_ThreadQuery(g_SqlTuple,"sql_piece_of_skins_thread"szQueryszData1);
}

public 
sql_piece_of_skins_thread(FailState,Handle:Query,Error[],Errcode,szData[],DataSize)
{
    if(
iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED
    { 
        
log_amx("%s"szError)
        return
    } 
    else 
    {
        new 
id iData[0]

        for(new 
i=0;<= MAX_SKINS;i++)
            
DB_skins[i][id] = SQL_ReadResult(Query/*Colum Num*/);
            
        
inventory(id);
    } 
}


public 
TestConsult(const id)
{
    
// Use register_clcmd to testing consult. Return console print
    
new iLen 0
    
new szQuery[256]

    
iLen += formatexszQueryiLen ], charsmaxszQuery ) - iLen"SELECT ")

    for(new 
i=0<= MAX_SKINS;i++)
        
iLen += formatexszQueryiLen ], charsmaxszQuery ) - iLen"`F%d`%s "i== MAX_SKINS "" ",")

    
iLen += formatexszQueryiLen ], charsmaxszQuery ) - iLen"FROM `accounts` WHERE `PlayerID`='%d'")

    
console_print(id"%s"szQuery)



Share the structure of the database "accounts"

krisztian2 07-04-2020 17:14

Re: loop sql select
 
Thank you for your reply, but i want to sum F0, F1.. F4 columns of players. And the results save to an array.

Natsheh 07-04-2020 18:26

Re: loop sql select
 
Use flags instead of strings for saving.


All times are GMT -4. The time now is 17:06.

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