Raised This Month: $ Target: $400
 0% 

How to combine some rows data into one string?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FEELiFE
Member
Join Date: Jul 2010
Old 04-22-2011 , 07:35   How to combine some rows data into one string?
Reply With Quote #1

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?
FEELiFE is offline
grimvh2
Veteran Member
Join Date: Nov 2007
Location: Fishdot Nation
Old 04-22-2011 , 07:44   Re: How to combine some rows data into one string?
Reply With Quote #2

PHP Code:
format(combinecharsmax(combine), "%s %s %s %s %s %s"word1word2word3word4word5word6
__________________
I am out of order!
grimvh2 is offline
FEELiFE
Member
Join Date: Jul 2010
Old 04-22-2011 , 10:50   Re: How to combine some rows data into one string?
Reply With Quote #3

Quote:
Originally Posted by grimvh2 View Post
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.
FEELiFE is offline
Old 04-22-2011, 10:59
Hunter-Digital
This message has been deleted by Hunter-Digital. Reason: meh, useless xD
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-22-2011 , 11:18   Re: How to combine some rows data into one string?
Reply With Quote #4

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++ ] = ' '
__________________

Last edited by Bugsy; 04-22-2011 at 11:22.
Bugsy is offline
FEELiFE
Member
Join Date: Jul 2010
Old 04-22-2011 , 14:28   Re: How to combine some rows data into one string?
Reply With Quote #5

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.
FEELiFE is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-22-2011 , 14:36   Re: How to combine some rows data into one string?
Reply With Quote #6

Are you asking how to do it with SQL or how to combine data that was retrieved?
__________________
Bugsy is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 04-22-2011 , 15:17   Re: How to combine some rows data into one string?
Reply With Quote #7

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

__________________
Impossible is Nothing

Last edited by Sylwester; 04-22-2011 at 18:27.
Sylwester is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-22-2011 , 16:56   Re: How to combine some rows data into one string?
Reply With Quote #8

Why use formatex() without any formatting? Use copy() or add().
__________________
Bugsy is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-22-2011 , 17:02   Re: How to combine some rows data into one string?
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 04-22-2011 , 18:35   Re: How to combine some rows data into one string?
Reply With Quote #10

Quote:
Originally Posted by Bugsy View Post
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] View Post
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
__________________
Impossible is Nothing
Sylwester is offline
Reply



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 20:10.


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