add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable?

$result = odbc_exec($connect, $query);
while ($row = odbc_fetch_array($result)) {
        echo $row["host"],"\n";
}
odbc_close($connect);
?>

This will output hostnames:

host1 host2 host3

I would like to add the output to the variable $hosts something like this:

$hosts = "host1,host2,host3"

I plan to use $host in another query further down in the script.

thanks & regards