Re: DBI CGI help

From: Jupiterhost.Net (mlists_at_jupiterhost.net)
Date: 05/17/04


Date: Sun, 16 May 2004 20:04:04 -0500
To: leegold <leegold@fastmail.fm>

leegold wrote:
> I have been looking for *clear, simple, upto date,
> working code examples* of using DBI and CGI.

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use CGI qw(header param);

> Requesting some action to be taken on a form
> from user and then returning the user's db select
> to them via the browser.

# WARNING : untested code ahead :) #
my $foo = param('foo');
print header();

if($foo) {
        my $c = 0;
        my $dbh = connect('your connect info here') or die DBI::errstr;
        my $fooQ = $dbh->quote($foo);
        my $records = $dbh->selectall_arrayref("SELECT Id, Bar FROM Stuff WHERE
Foo = $fooQ");
        print "Your Foo will be <ul>\n";
        for(@{ $records }) {
                $c++;
                my($id, $bar) = @_;
                print "<li>$id $bar<li />\n";
        }
        print "</ul><br />I found $c Foo(s)<br />\n";
        $dbh->disconnect();
}

print <<FORM;
        <form action="this.pl" method="post">
          Enter Your Foo <input type="text" name="foo" value="$foo" />
          <input type="submit" value="Find Foo" />
        </form>
FORM

> I been trying all day on the web and I have
> alot of fragmented stuff but nothing definative.
>
> Does anyone know of anything? I'd prefer something
> simple and recent. Thanks.
>

I know there are plenty of things I could improve in the code above but
that should get you started :)

perldoc DBI
perldoc CGI

HTH

Lee.M - JupiterHost.Net



Relevant Pages

  • Re: DBI CGI help
    ... working code examples* of using DBI and CGI. ... Requesting some action to be taken on a form ...
    (perl.dbi.users)
  • Re: Make database handle persist in CGI/DBI application?
    ... Your CGI is called for each request and then terminated. ... There is no way to make the DBI handle persistant. ... You need a permanently running server for a persistant handle. ... I have tried passing the handle in as a ref parameter, using $dbhRef = /*dbh ...
    (perl.dbi.users)
  • Re: niranjani
    ... i have to use for we enabled work using html for that which cgi version ... tp be used and only dbi module is enough or else dbd:odbc also required ... How do I install DBI on Windows for Perl 5.6.1? ...
    (perl.dbi.users)
  • Re: new CGI::Session creates a new session every visit. GRRR!!!
    ... it seems to be something screwy with a combination of DBI and global ... my $cgi = new CGI; ... I want my $cgi and $session variables to be ...
    (comp.lang.perl.misc)
  • RE: How to track the success of insert
    ... > How will I track if the insertion is success or not? ... > use CGI; ... > use DBI; ... Put your instert inside a transaction like this: ...
    (perl.beginners)