Solved

Help needed with converting a query result set into variables.

Posted on 2007-04-05
5
167 Views
Last Modified: 2008-03-06
I think what I want to do is possible.  I have the following result set from a query and want to be able to create a comma separated list for each field's result set.

Field1   Field2   Field3
--------------------------
Data1   Name1  Fr
Data2   Name2  Fr

And the desired result is a variable that contains each field's data is a comma separated list

$Field1Var = Data1, Data2
$Field2Var = Name1, Name2
$Field3Var = Fr

Can anyone help me with this?  Thanks!
0
Comment
Question by:Quetysis
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 18859840
     $Field1Var = "";
      $Field2Var = "";
      $Field3Var = "";
      
      $query = mysql_query("SELECT * FROM tablename");
      
      $i = $k = $j = 0;
      
      while($row = mysql_fetch_object($query)) {
      
            if(!strstr($Field1Var,$row->Field1)) {
            
                  $Field1Var .= ($i==0?$row->Field1:", ".$row->Field1);
            
                  $i++;
            }
            
            if(!strstr($Field2Var,$row->Field2)) {
            
                  $Field2Var .= ($k==0?$row->Field2:", ".$row->Field2);
            
                  $k++;
            }
            
            if(!strstr($Field3Var,$row->Field3)) {
            
                  $Field3Var .= ($i==0?$row->Field3:", ".$row->Field3);
            
                  $j++;
            }
      
      
      
      
      }
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 18859846
change $Field3Var .= ($i==0?$row->Field3:", ".$row->Field3);

to

$Field3Var .= ($ξ==0?$row->Field3:", ".$row->Field3);
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 18859851
emm lol ..... sorry keyboard switch to greek :)

$Field3Var .= ($j==0?$row->Field3:", ".$row->Field3);
0
 

Author Comment

by:Quetysis
ID: 18859957
Ah!  Thanks steelseth12!  I'm going to try this out and report back to you.
0
 

Author Comment

by:Quetysis
ID: 18860392
That worked beautifully.  Thanks again!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now