|
Posted by Steven Paul on 10/19/06 23:22
I need to write the output of an SQL query as comma-separated values.
So I wrote this, which is almost right:
while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
{
$rows[] = $row;
}
foreach( $rows[0] as $key => $value)
{
echo $key.",";
}
echo "\n";
foreach ($rows as $row)
{
foreach( $row as $key => $value)
{
echo $value.",";
}
echo "\n";
}
It first writes a header row, then all the data rows below it. The only
problem is the trailing comma on each line. Is there some clever way of
knowing when I'm on the last element, and so not add the last comma?
If it's not already blindingly obvious, I'm new to PHP ;-)
Thanks,
-Steve
[Back to original message]
|