AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to combine some rows data into one string? (https://forums.alliedmods.net/showthread.php?t=155449)

FEELiFE 04-22-2011 07:35

How to combine some rows data into one string?
 
Hi guys! I have a table in mysql database. I get user's info with:

Code:

SELECT * FROM `table` WHERE steamid='%s'
And the query returns some rows. I want to combine the values from column in all rows.

Example: (these are the values from the column in all returned rows)
1. a
2. b
3. c
4. d
5. e
6. f

And I want to combine them all in one string:
Code:

new combine[32] = "abvdef"
How canI do this?

grimvh2 04-22-2011 07:44

Re: How to combine some rows data into one string?
 
PHP Code:

format(combinecharsmax(combine), "%s %s %s %s %s %s"word1word2word3word4word5word6


FEELiFE 04-22-2011 10:50

Re: How to combine some rows data into one string?
 
Quote:

Originally Posted by grimvh2 (Post 1455325)
PHP Code:

format(combinecharsmax(combine), "%s %s %s %s %s %s"word1word2word3word4word5word6


Yeah man.. I know how to combine them, but there are different number of rows for different players, so I don't know how to get them ALL and combine them. The rows can be 5 or 10 or 15 or more or less..

So one more time - I get all rows I need with:
SELECT * FROM `table` WHERE steamid='%s'
then from each row, I want to get the value from the column test, and finally to combine all values in one string.

Bugsy 04-22-2011 11:18

Re: How to combine some rows data into one string?
 
When you retrieve a single value, add it to the string.
PHP Code:

new szDest255 ] , iPos;

//This will add szValue to szDest, you can use the same line over and over to add more data.
iPos += addszDestiPos ] , charsmaxszDest ) , szValue );

//If you need to add a space or any other char between strings.
szDestiPos++ ] = ' '


FEELiFE 04-22-2011 14:28

Re: How to combine some rows data into one string?
 
I can't really get it. How can I retrive the data from the column of all rows? Can you give me an example, please?

If szValue equals to SQL_ReadResult(Query, 3) it only gets the data from the first row. I want to combine the data from all returned rows.

Bugsy 04-22-2011 14:36

Re: How to combine some rows data into one string?
 
Are you asking how to do it with SQL or how to combine data that was retrieved?

Sylwester 04-22-2011 15:17

Re: How to combine some rows data into one string?
 
PHP Code:

public load(id){
    new 
cache[128]
    
formatex(cache,127,"SELECT * FROM mytable;")
    
SQL_ThreadQuery(g_SqlTuple"handle_load"cache)
}

public 
handle_load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    new 
data[32], tmp[512], pos 
    
while(SQL_MoreResults(Query)){
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"column_name"), data31)
        
pos += formatex(tmp[pos], 511-posdata)
        
SQL_NextRow(Query)
    }
    
log_amx("combined data: %s"tmp)
    return 
PLUGIN_HANDLED



Bugsy 04-22-2011 16:56

Re: How to combine some rows data into one string?
 
Why use formatex() without any formatting? Use copy() or add().

Exolent[jNr] 04-22-2011 17:02

Re: How to combine some rows data into one string?
 
Quote:

Originally Posted by Bugsy (Post 1455659)
Why use formatex() without any formatting? Use copy() or add().

Also, pos is never incremented, so it will always be set to the beginning of the string, resulting in tmp containing only the last row's data.

Sylwester 04-22-2011 18:35

Re: How to combine some rows data into one string?
 
Quote:

Originally Posted by Bugsy (Post 1455659)
Why use formatex() without any formatting? Use copy() or add().

I don't know if he is going to retrieve strings or integers, so he may change it to formatex(... "%d", data), besides if you are claiming that there is any significant difference in performance then provide some proof.
Quote:

Originally Posted by Exolent[jNr] (Post 1455664)
Also, pos is never incremented, so it will always be set to the beginning of the string, resulting in tmp containing only the last row's data.

fixed


All times are GMT -4. The time now is 20:10.

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