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


In reply to Database Downloads by steveAZ98

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.