0

I have a collection of results from a MySQL query being fetched in a loop. I need to store them as a single variable with a space in between each result.

$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
    echo $row['Names'] . " ";
}

So later, I can call 'echo $Names;' and get the string of names with a space in between.

ex) Clinton Bush Huckabee Romney etc....

Thanks for your help!

4 Answers 4

1
$Names = '';
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
    $Names.= $row['Names'] . " ";
}
Sign up to request clarification or add additional context in comments.

Comments

1
$result = mysql_query("SELECT Names FROM table");

$names = "";

while($row = mysql_fetch_array($result)){
    $names .= $row['Names'] . " ";
}

echo $names;

Comments

1

First of all, please, stop using mysql extention. It's deprecated. Try mysqli or PDO.

$names = array();
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
    $names[] = $row['Names'];
}
echo implode(' ', $names);

Comments

0

I would use an array and then implode it using a space:

$result = mysql_query("SELECT Names FROM table");
$arr = array();
while($row = mysql_fetch_array($result)) $arr[] = $row['Names'];
echo implode(' ', $arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.