Ok, I work for a company that is constanly needs to download information from a database that collects information from forms. Every time they would need something they would come to me and ask for what they needed. This quickly got annoying so I wrote this quick little script that creates a comma seperated file. Some servers allow a download, some you'll have to copy and paste from the browser.

#!/usr/bin/perl -w

use CGI;
use DBI;
my $q = new CGI;

my $user = ''; # Be sure to add your username and password here
my $pass = '';

my $db = $q->param('db');
my $table = $q->param('table');
my $fields = $q->param('fields') || '*';
my $where = $q->param('where');
my $order = $q->param('order');
my $dbh = DBI->connect("DBI:mysql:$db","$user","$pass");

my @valid = qw( ); # ip's that are allowed in here
my $from = $ENV{'REMOTE_ADDR'};

my $x = 0;
if (($db)&&($table)) {
        foreach my $i (@valid) {
                if ($from =~ /$i/i) {

                        my $sth = $dbh->prepare(qq! SELECT $fields FROM $table $where $order !);
                        $sth->execute();
                        my @fld = @{ $sth->{NAME} };
                        my $ar = $sth->fetchall_arrayref();
                        $sth->finish();
                        my $rows = (!$ar ? 0 : scalar @{$ar});

                        print "Content-type: \r\n\r\n";
                        my $flds = join '","', @fld;
                        print "\"$flds\"\r\n";
                        if ($rows > 0) {
                                for my $i (0 ..$rows - 1) {
                                        my $vals = join '","', @{ $ar->$i };
                                        print "\"$vals\"\r\n";
                                }
                        }

                        $x++;
                }
        }
}

if ($x == 0) {
                # invalid user
                my $q = new CGI;
                print $q->header(), $q->start_html(-title=>'Illegal User');
                print "<div align=center><h2>Invalid Page</h2>";
                print "<br><br>";
                print "The page accessed is not a valid page.<br>";
                print "<br><a href=\"/index.htm\">Return Home</a>";
                print "<br></div>";
                print $q->end_html;
}

$dbh->disconnect();
exit;

link to it like this: http://server/cgi-bin/db.csv?db=dbname&table=tablename

Replies are listed 'Best First'.
RE: Database Downloads
by SuperCruncher (Pilgrim) on Jul 22, 2000 at 14:17 UTC
    my $dbh = DBI->connect("DBI:mysql:$db","$user","$pass");

    <pedantic> quoting $user and $pass like that isn't really necessary, as far as I know. See perlfaq that says "What's wrong with always quoting "$vars"?". </pedantic>

      Cool, thanks for the tip. I just added that for the post because my original script had the username and password hardcoded in single quotes.
RE: Database Downloads
by lachoy (Parson) on Aug 11, 2000 at 01:40 UTC
    A couple small things:

    1) You can use the content-type 'text/csv' and most browsers will popup a window prompting the user to save the file. Since you've already named the file 'xx.csv', most browsers will pop that name into the dialog as the default name.

    2) Depending on your data, you might need to escape any commas that are in the fields. You can do this by hand, or you might want to simply use DBD::CSV, which will take care of everything for you.

    Hope this helps!