Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
HI
Here's the Perl code
#!/usr/bin/perl use strict; use DBI; use CGI; my $DB_Host = 'localhost'; my $DB_Name = 'widgets'; my $DB_User = 'root'; my $DB_Pass = 'xyz123'; { # begin main #-- # Create a new CGI object to process the HTML form. my $cgi = new CGI; #-- # Get the value of the "LastName" field from our HTML input form. my $lastNameToQuery = $cgi->param('LastName'); #-- # Get a connection to the database server, aborting on failure. my $dbh = openDBConnection($DB_Host, $DB_Name, $DB_User, $DB_Pass); if ( ! defined $dbh ) { printErrorPage("Could not open a connection to the database!"); exit; } #-- # Define the query, prepare it, execute it then get the results as a +n array of hash references. my $sql = "SELECT * FROM names WHERE Last = \"" . $lastNameToQuery . + "\""; my $sth = $dbh->prepare($sql); $sth->execute(); my $queryResults = $sth->fetchall_arrayref({}); #-- # Print the results as an HTML table. # # Note: the {'Last'} and {'First'} found below refer to column names + in the database table. print STDOUT "Content-Type: text/html\n"; print STDOUT "\n"; print STDOUT "<TABLE BORDER=\"1\">" . "\n"; print STDOUT "<TR><TH>Last Name</TH><TH>First Name</TH></TR>" . "\n" +; foreach my $i (0 .. $#$queryResults) { print STDOUT "<TR><TD>" . $queryResults->[$i]->{'Last'} . "</TD><T +D>" . $queryResults->[$i]->{'First'} . "</TD></TR>" . "\n"; } print STDOUT "</TABLE>" . "\n"; #-- # Close our database connection. $dbh->disconnect(); } # end main # ==================================================================== +===== # F U N C T I O N D E F I N I T I O N S # ==================================================================== +===== # -------------------------------------------------------------------- +----- # | # | Function : printErrorPage # | # | Parameter : $errorMessage = the error message to print # | # | Return : none # | # | Comments : Prints an HTML error message. This prints the "Conten +t-Type" # | HTTP header. # | # -------------------------------------------------------------------- +----- sub printErrorPage { my $errorMessage = shift; print STDOUT "Content-Type: text/html\n"; print STDOUT "\n"; print STDOUT "<H1>" . $errorMessage. "</H1>\n"; } # -------------------------------------------------------------------- +----- # | # | Function : openDBConnection # | # | Parameter : $host = the name or ip of the database server # | Parameter : $db = the name of the database to connect to # | Parameter : $username = the username to use when connecting to the + MySQL server # | Parameter : $password = the password to use when connecting to the + MySQL server # | # | Return : A handle to the database connection. # | # | Comments : # | # -------------------------------------------------------------------- +----- sub openDBConnection { my $host = shift; my $db = shift; my $username = shift; my $password = shift; my $dbh; # Handle to a database connections. $dbh = DBI->connect("DBI:mysql:$db:$host", $username, $password, { RaiseError => 0, AutoCommit => 1 } ); if ( ! defined ($dbh) ) { return undef; } else { return $dbh; } }
I hope it could help you

In reply to Re^2: Help with CGI and pl by ArmandoG
in thread Help with CGI and pl by ArmandoG

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found