Create a variable for while loop results [duplicate]
while ($topic = mysql_fetch_assoc ($result)); {
echo "{$topic["overtag "]} ";
}
The results from my while loop are displaye开发者_JAVA百科d 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?
concatenation operator .=
$fruits = '';
while ($topic = mysql_fetch_assoc ($result)); {
$fruits .= "{$topic["overtag "]} ";
}
// 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)
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)) {
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";
}
精彩评论