Re: Send .csv file to browser [message #170049 is a reply to message #170048] |
Thu, 07 October 2010 12:41 |
sheldonlg
Messages: 166 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 10/7/2010 8:19 AM, "Álvaro G. Vicario" wrote:
> El 07/10/2010 14:08, Derek Turner escribió/wrote:
>> On Thu, 07 Oct 2010 10:25:50 +0000, Derek Turner wrote:
>>
>>> <snip very useful material>
>>>
>>> Many thanks, Jerry, I'll give it a go and report back.
>>
>> <?php
>> include("/home/cantabi1/admin_connect.php");
>> $sql = "SELECT `givenName`, `familyName`, `email`, `mobile`, `landLine`
>> FROM `member` WHERE `active` = 1";
>> $result = mysql_query($sql, $conn) or die (mysql_error());
>> if ($result) {
>> $outfile = fopen('php://output', 'w');
>> header('Content-Type: text/csv');
>> header('Content-disposition: attachment; filename=data.csv');
>> while ($row = mysql_fetch_array($result)) {
>> fputcsv($outfile, $row);
>> }
>> fclose($outfile);
>> }
>> ?>
>>
>> Produces a csv file just fine BUT each variable is doubled up i.e.
>> Derek,Derek,Turner,Turner, etc. can anyone see why?
>
> Try this and you'll see why:
>
> while ($row = mysql_fetch_array($result)) {
> print_r($result);
> }
>
> You can just use mysql_fetch_row().
>
>
Good point! From the manual (www.php.net):
array mysql_fetch_array ( resource $result [, int $result_type =
MYSQL_BOTH ] )
so the default is for both.
This would work if you added MYSQL_NUM as the optional second argument
-- or as you correctly stated just use mysql_fetch_row.
--
Shelly
|
|
|