I don't know how much experience you have in perl, web and database, so here's the basics - the requisites and a simple script to get you started. You need:

The html page comprises a form with a method, and some inputs. The browser sends the input names along with their values to the webser server, which parses the request and provides the name/value tupels to the script, depending on the request method:

You take these request values and query the database with them, then you send the results back.

So, you could roll your own parsing of the query like so:

my $query; if ($ENV{REQUEST_METHOD} eq 'GET') { $query = $ENV{QUERY_STRING}; } elsif ($ENV{REQUEST_METHOD} eq 'POST') { read STDIN, $query, $ENV{CONTENT_LENGTH},0; } # make a hash from the query my %param; my @tupels = split /(\&|;)/,$query; for my $tupel(@tupels) { my ($key,$value) = split /=/, $tupel; $param{$key} = $value; }

but this is highly discouraged since this basic code, provided as an example only doesn't handle all edge cases (e.g. multivalue parameters) and could make your script vulnerable. Better (not best, though) is using the CGI module.

The following script provides a simple form, and the results of the query, if valid POST data has been sent along with the request.

You need a table persons in the test database on your SQL server, like so:

CREATE TABLE persons ( name varchar(255), age tinyint );

and of course, fill it with some values.

Change the username and password below to reflect your setup.

#!/usr/bin/perl -T # always use -T for CGI stuff. read perlsec use strict; use warnings; use CGI qw(:standard); # this pulls in param(), header() and start_htm +l() use DBI; my @query_results; my $error; my $name; # if we have a 'name' param, check it... if (param('name')) { if (param('name') =~ /^(\w+)$/) { $name = $1; } # ...and if name param is ok query the DB if ($name) { my $dbh = DBI->connect( "DBI:mysql:database=test", 'testuser', # username - change to your setup 'secretpw', # password - change to your setup {RaiseError => 1}, ) or $error = "could not connect to database"; my $sth = $dbh->prepare( "select name, age from persons where name = ?" ); $sth->execute($name); while(my $row = $sth->fetchrow_arrayref) { push @query_results, $row; } } } else { $error = "please enter a valid name\n"; } print header(), start_html('SQL Query'); print <<EOH; <html> <head><title>SQL</title></head> <body> <h1>SQL Query</h1> <form method="POST" action="/cgi-bin/sql.cgi"> <input id="name" name="name"/> </form> <hr> <table border=1> <thead><tr><td>Name</td><td>Age</td></tr></thead> <tbody> EOH foreach my $row (@query_results){ my ($name, $age) = @$row; print "<tr><td>$name</td><td>$age</td></tr>\n"; } print "</tbody>\n</table>\n"; print "<b>$error</b>" if $error; print "</body></html>\n";

See perlsyn, perldata, perlvar, perlsec; read perfunc, perlref, perlreftut ...well, read the complete perl documentation ;-). Read the CGI and DBI manual pages.

There are complete web frameworks to be found on the web for CRUD operations (create/read/update/delete) which you should consider if your project comprises more than simple queries, and as your familiarity with perl grows.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Need GUI for easy query and results by shmem
in thread Need GUI for easy query and results by TCLion

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.