1
while ($topic = mysql_fetch_assoc ($result)); {
   echo "{$topic["overtag "]} ";
}

The results from my while loop are displayed as such: apples orange banana

I want to be able to take all those results and put them in a variable and make it look like this: $fruits = apples orange banana

How would I be able to do this?

1
  • What have you tried so far? Where are you stuck? How is this problem directly related to MySQL? Commented Dec 9, 2022 at 15:48

4 Answers 4

4

concatenation operator .=

$fruits = '';
while ($topic = mysql_fetch_assoc ($result)); {
    $fruits .= "{$topic["overtag "]} ";
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is my preference $result .= $rows[ 'name' ];
This code would throw an obvious syntax error
3
// I love arrays.
$fruits = array();
while ($topic = mysql_fetch_assoc ($result)); {
   $fruits[] = (string)$topic["overtag "];
}

// If you don't want an array, but a string instead, use implode:

$fruits = implode(' ', $fruits)

3 Comments

Thanks GolezTrol,but when I do this, the result just says "Array", my actual array does not come up.
I got rid of the [] brackets after $fruits on line [] 3 and that gave me the results that I wanted, but only when I echoed inside the while loop. When I echo $fruits outside of the while loop, I only get one of the results, meaning I only get "apples" instead of "apples orange banana"
You can implode the array if you like, using implode as demonstrated in the line of code under the loop. You could of course just concatenate the strings, but in in practise in many cases you'll need to use the independent values that are retrieved from a database, that's why I put them in an array. Arrays are very powerful tools to keep lists of data in. In cases where you'd like to convert the values in a array to a single string, you can use implode.
1

You merely need to concatenate each one onto the variable inside the loop

$fruits = "";
while ($topic = mysql_fetch_assoc ($result)); {
  echo "{$topic["overtag "]} ";
  $fruits .= $topic['overtag'] . " ";
}
// This is going to result in an extra space at the end, so:
$fruits = trim($fruits);

Oh, also, you have an errant semicolon which is going to break your while loop:

while ($topic = mysql_fetch_assoc ($result)); {
                                   --------^^^--

Should be:

while ($topic = mysql_fetch_assoc ($result)) {

Comments

1

Using the PHP code below, you can get data from database table and display on webpage:

    $sql_query="select * from yourTable";   
    $result=mysqli_query($connection,$sql_query); 
    if(mysqli_num_rows($result) > 0)
    { 
      while($row = $result->fetch_array(MYSQLI_ASSOC))
      { 
        echo "ID ".$row[0];//echo "ID ".$row["ID"];
        echo "Name ".$row[1];//echo "Name ".$row["Name"];
       }
    }
    else
    {
      echo "No Record";
    }

1 Comment

Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.