Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi,
i'v just started learning perl and have a project which requires mysql in the backend. To start with i need to display all the rows of a table (select* from table) in an html page and the scripting needs ot be in perl. I've managed to create a perl script which looks like this:#!/usr/bin/perl
use DBI qw(:sql_types);
my ($databaseName, $host, $databaseUser, $databasePw, $dbh);
my ($stmt, $sth, @newRow);
my ($counter);$databaseName="@@@@";
$host="@@@@@";
$databaseUser="user";
$databasePw="password";my $dbh = DBI->connect("DBI:mysql:database=$databaseName:host=$host", $databaseUser, $databasePw) or die "Can't connect to database:$DBI::errstr\n";
print("Successfully connected to MySql Database.");$stmt = "SELECT * FROM table";
$sth = $dbh->prepare($stmt) || die "prepare: $$stmt: $DBI::errstr";
$sth->execute || die "execute: $$stmt: $DBI::errstr";@record = $sth->fetchrow();
$counter = $record[0];
$sth->finish();
$dbh->disconnect();But the problem is how to convert it into a cgi script to be placed in a web server, so that after clicking a button for example, the sql query in the code is displayed in the html page..i am clueless about th html code to be written
Please help
naveen

Call the Perl script directly! Virtually any executable file whether it be a script written in Bash, Perl, Python, etc. or an executable such as a compiled binary can be called as a CGI. The things you need to be aware of are the permissions, the mode or attributes of the file, and the Webserver settings that specify location and filetypes (or extensions) that are allowed to be called and executed as CGIs. I've written CGIs in Bash, Perl, Sed, etc. with much success. They all work very well but the Webserver has to be configured to allow them. So first off, I'd save your file as whatever.cgi and place it in your cgi-bin dir (or whatever your Webserver specifies the scripts directory to be), make sure its attributes are set to executable (chmod 755 whatever.cgi) and ownership is correctly set, and give it a try. If it doesn't work, go to your server logs and see if there are any clues. Most likely you'll run into things like script execution disallowed from current location, script extension not set as one of the allowed extension types, or just syntax errors within the script when it does try to run. Once you get that far, you're nearly there. Just a little debug of your script and you'll have it! Again, check your logfiles. They'll be full of clues. Good luck!
Rob
http://rob.pectol.comRob Pectol
http://rob.pectol.com/

thanks rob for the reply. but what i intented to know more was that what html code should i embed in this script so that it shows the database entries in the webpage?? Please se if you can help me out with this.

I waould assume that you want to show the data in a table. I'll give you some basics, but there are tons of tutorials and references regarding tables on the net.
You start a table with the tag <table> and end the table with the tag </table>. Similarly you start and end a row with <tr> and </tr>. And, lastly you create a cell with the following <td> & </td>.
So let's say you want to create a table that displays sales and profits for three different years. You would need three columns (one for the years and two for the data) and four rows (one for the column headers and three for the data). The code would look something like this:
<table>
<tr>
<td>Year</td>
<td>Sales</td>
<td>Profits</td>
</tr><tr>
<td>2000</td>
<td>$650,000</td>
<td>$25,000</td>
</tr><tr>
<td>2001</td>
<td>$786,000</td>
<td>$33,000</td>
</tr><tr>
<td>2002</td>
<td>$845,000</td>
<td>$40,000</td>
</tr></table>
Michael J
You need to be careful to always have the same number of cells (TD tags) OR be sure to use the span property to force a cell to span multiple columns. Otherwise you may have some problems with the table getting skewed.

I think what you need is following.
If you have a submit, you can call the perl script directly as FORM action as follows.
<form method="POST" action="/cgi-bin/name_of_perl_scrip.pl">
<input type="submit" value="Execute SQL"></form>
Once the user clicks the Execute SQL button, the perl script is triggered. now make sure in your httpd.conf you mention the script execution location as cgi-bin, there are good permission on the directory level, script has execute permission.
Also if you want your perl script to generate the HTML page for you, you can use following.
If you know how many columnts you want to show to the user,
print <<HTML;
<HTML>
and rest of html tags, till you reach the data
HTMLprint $counter; # the count you have received from database
print <<HTML;
this is where you have HTMLtags closing.
</HTML>
HTMLHope this helps.
regards

thanks cdac,
i've been able to establish mysql connectivity to display all the data in in an html page by invoking a perl script in the web server. But tough things ahead. I've trying uselessly for hours to create a script which takes username and password from the user and validate them with those in mysql table and return a response in either situation but not being able to do it.
my script looks somewhat like----#!/usr/bin/perl
use CGI;
use DBI;
my $query = new CGI;
print $query->header();
#print $query->start_html(-title=>'LOGIN PAGE');$db="db";
$host="host";
$user="root";
$password=undef;if(($query->request_method() eq "POST") && ($query->param("user") ne '') && ($query->param("password") ne ''))
{
$user=$query->param("user");
$password=$query->param("password");
$dbh = DBI->connect("DBI:mysql:database=$db:host=$host", $user, $password) or die "Can't connect to database:$DBI::errstr\n";
$qry = $dbh->prepare("SELECT emp_name, password FROM table where emp_name=$user");
$qry->execute;
@row=$qry->fetchrow_array;
$name=$row[0];
$password=$row[1];if ($user eq $name && $password eq $pass)
{
print "Thank You!
";
$dbh->disconnect;
print $query->end_html();
}
else
{
print "<HEAD> Username or Password Incorrect!</HEAD>";
print "Please Try Again";$dbh->disconnect;
print $query->end_html();
}
}
else
{
#print "Content-type: text/html\n\n";
#print <<EOF;
print "<HTML>";
print "<BODY>";
print "<FORM METHOD='POST' ACTION='login_main.pl'>";print "
Username:</p>";
print "<input type='text' name='user'>
";print "
Password:</p>";
print "<input type='password' name='password'>
";
print "<input type='submit' value='submit'>
";
print "</FORM>";
print "</BODY>";
print "</HTML>";
#EOF
#print $query->end_html();
}not getting the desired output. basically a problem of data validation with db.
Could anyone help me out with this one!!
Thanks a lot
Naveen

Before I start to look at re-working your code, add these lines to your script to get additional info.
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
warningsToBrowser(1); # this should be put after printing the header;

I think i've sorted the previous problems... but theres one big ahead..!! inspite of typing the correct password i'm getting the message for 'wrong password'. I suppose this is coz the passwords are md5'd in the db and i'm not connecting as su. So what to do now?? even if i get su access, what code i'll have to write to authenticate against the encrypted password?? PLease help
thanks
naveen

You'll need to decrypt the db password or encrypt the user supplied password prior to doing the comparision. Here's a modified version of your script that uses the Digest::MD5 module and seperates the code up into 2 subroutines (normally I'd break it down a little more) which I think make it easier to read/write/debug.
This has not been tested/debugged but it's a good starting point.
#!/usr/bin/perl -wuse strict;
use DBI;
use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $q = new CGI;
my %login = $q->Vars;print $q->header();
print $q->start_html(-title=>'LOGIN PAGE');
warningsToBrowser(1);login_form();
db_query($login{'user'}, $login{'password'}) if (defined $login{'user'} && defined $login{'password'});
sub login_form {
print start_form();
print "Username: <input type='text' name='user'>";
print "Password: <input type='password' name='password'>";
print "<input type='submit' value='submit'>";
print end_form();
}sub db_query {
my $db = 'db';
my $host = 'host';
my $user = 'root';
my $password = '';
my ($usr, $pass) = @_;
my $digest = md5($pass);
# my $digest = md5_hex($pass);
# my $digest = md5_base64($pass);my $dbh = DBI->connect("DBI:mysql:database=$db:host=$host", $user, $password) or die "Can't connect to database:$DBI::errstr\n";
my $qry = $dbh->prepare("SELECT emp_name, password FROM table where emp_name=$user");
$qry->execute;
my ($name, $password) = $qry->fetchrow_array;
$dbh->disconnect;
die "Query Failed" unless ($name && $password); # chg this to call a custom exit subroutine
if ($name eq $usr'} && $password eq $digest) {
print " Thank You!";
}
else {
print "Please Try Again";
}
print $query->end_html();
}

Here's a link to the cpan doc for the Digest::MD5 module.
<a herf="http://search.cpan.org/~gaas/Digest-MD5-2.33/MD5.pm">Didest::MD5

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |