For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2004 > howto: download mysql contents









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author howto: download mysql contents
Greg Brewer

2004-03-27, 11:53 pm

I have read about and successfully designed a web page to update a file to
the server. I am having some problems with it but nothing I have given up
solving.

Right now, I am working on another problem unrelated to the upload
problem -- I want to download selected contents of a table. I know how to
extract the data I want and I am reasonably sure I can write it to a csv
file. I can't figure out how to download it.

My first thought was to use an anchor tag (ie <A...> ) that referenced the
file. The problems with that are that I want a button and the file doesn't
exist prior to the click. I'm stuck. I could attach a javascript function
to the onclick handler for the anchor tag but javascript cannot create a
file. I could use the php form handler to write the data to a file but then
I don't know how to download it. I thought about placing an anchor tag on
the window with style="display: none" then activating it after the file is
created using it's click() method; however, 1) it doesn't appear to have a
click() method, and 2) the browser (my IE 6.0 at least) insists on
displaying the csv file. Funny thing is that if it were an exe file then
problem 2 wouldn't exists. I've played around a little with the anchor tag
trying to get it handle this file the same as if it were .exe using the type
attribute: nothing doing though. It did handle it the way I want if I
renamed the file from .csv to .exe. But I don't want to mislabel the type
of the file.

Does anyone have any ideas on how to handle this?

Greg


Colin McKinnon

2004-03-28, 10:10 pm

Greg Brewer spilled the following:

>
> Right now, I am working on another problem unrelated to the upload
> problem -- I want to download selected contents of a table. I know how to
> extract the data I want and I am reasonably sure I can write it to a csv
> file. I can't figure out how to download it.
>


in the file referenced by the 'action' of the form put something like:

<?php

header ('Content-type: application/x-csv');

$db=mysql_connect();

$qry=generate_query_with_posted_params(...); // your function
$res=mysql_query($qry);

while ($d=mysql_fetch_row($res)) {
print implode(',', $d) . "\n";
}
?>

HTH

C.
Greg Brewer

2004-03-28, 10:10 pm

YES!!!

Follow up question: How can a control the name used for the file on
download? Right now, it uses the name of the php file. I assume I have to
use xxx.php for the filename of "the file referenced by the 'action'. But
that means the filename used when saving is xxx.php instead of data.cvs.

Thanks,
Greg

"Colin McKinnon"
<colin.thisisnotmysurname@ntlworld.deletemeunlessURaBot.com> wrote in
message news:g7B9c.163$I8.82@newsfe1-gui.server.ntli.net...
> Greg Brewer spilled the following:
>
to[color=darkred]
>
> in the file referenced by the 'action' of the form put something like:
>
> <?php
>
> header ('Content-type: application/x-csv');
>
> $db=mysql_connect();
>
> $qry=generate_query_with_posted_params(...); // your function
> $res=mysql_query($qry);
>
> while ($d=mysql_fetch_row($res)) {
> print implode(',', $d) . "\n";
> }
> ?>
>
> HTH
>
> C.



Colin McKinnon

2004-03-29, 4:31 pm

Greg Brewer spilled the following:

> YES!!!
>
> Follow up question: How can a control the name used for the file on
> download? Right now, it uses the name of the php file. I assume I have
> to
> use xxx.php for the filename of "the file referenced by the 'action'. But
> that means the filename used when saving is xxx.php instead of data.cvs.
>


That's browser side and (AFAIK) really out of the control of PHP. Although
you could fudge your webserver to consider .csv (NOT CVS - thats something
completely different) as php scripts, then rename myscript.php myscript.csv

C.
Greg Brewer

2004-03-29, 8:31 pm

That's disappointing. There must be some way to control the name of the
file the browser uses. When I download non-viewable files (eg .zip files),
the browser defaults to the name of file. In my original trial, I had a
little bit of success by putting the php file as index.php and putting in a
data.cvs folder; unfortunately I got the name but not the extension. I wish
there was some I could review the headers but I'm stumped.

Greg

"Colin McKinnon"
<colin.thisisnotmysurname@ntlworld.deletemeunlessURaBot.com> wrote in
message news:l30ac.8$B36.4@newsfe1-win...
> Greg Brewer spilled the following:
>
But[color=darkred]
>
> That's browser side and (AFAIK) really out of the control of PHP. Although
> you could fudge your webserver to consider .csv (NOT CVS - thats something
> completely different) as php scripts, then rename myscript.php

myscript.csv
>
> C.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com