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?

Naveed
  • 41,517
  • 32
  • 98
  • 131
Samantha
  • 29
  • 1
  • 2
  • 3

4 Answers4

4

concatenation operator .=

$fruits = '';
while ($topic = mysql_fetch_assoc ($result)); {
    $fruits .= "{$topic["overtag "]} ";
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
David Chan
  • 7,347
  • 1
  • 28
  • 49
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)
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Thanks GolezTrol,but when I do this, the result just says "Array", my actual array does not come up. – Samantha Jun 04 '11 at 15:20
  • 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" – Samantha Jun 04 '11 at 15:34
  • 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`. – GolezTrol Jun 04 '11 at 21:23
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";
    }
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
Wajid khan
  • 842
  • 9
  • 18
  • Welcome to Stack Overflow! While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) 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. – yivi Jan 19 '18 at 10:53
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)) {
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390