Milksnake12
Milksnake12

Reputation: 561

Query results stored into one variable

I've read through some other posts that were similar but I can't seem to get a good implementation of them. I'm calling a php script from another program that needs the results returned in one variable, space or comma separated. The php connects to a db (no problem there) and runs a query that will return 2 to 6 or so matching rows. I need those results together in one variable but can't seem to get it.

Here's where I'm stuck.

$t = "SELECT user FROM call_times WHERE client='$clientid' AND start <= $date AND end >=          $date";
$result = mysql_query($t) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {

$temp = $row['user'];
}
echo $temp;

The query runs fine, but you can see from the code what I'm trying (and failing) to do in the lower part. I need $temp to hold a list of results (ex: 5567889 57479992 4335780 (each of which is a different entry in user column)).

Thanks so much!

Upvotes: 0

Views: 276

Answers (2)

Matt
Matt

Reputation: 7050

Try group_concat() and you won't need PHP to manipulate the results.

Upvotes: 1

David
David

Reputation: 3821

$array = array();
while ($row = mysql_fetch_array($result)) {
   $array[] = $row['user'];
}

$string = implode(" ", $array); 

Now $string has the space-separated values of the user column.

Good luck!

Upvotes: 3

Related Questions