Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   MySQL-Perl Connectivity (http://www.go4expert.com/articles/mysql-perl-connectivity-t275/)

pradeep 20Apr2005 23:43

MySQL-Perl Connectivity
 
Connect to a MySQL server using Perl.
In order to do that you'll have to use the DBI module in Perl.
Below you will find an example code.

Code: PERL

#!/usr/local/ActivePerl-5.6/bin/perl --

print "Content-type: text/html\n\n";

print "<html><head><title>Database Test Page</title></head><body bgcolor=black text=yellow>";

use DBI;

#dbh=DBI->connect("DBI:mysql:dbname:server","username","password");
$dbh=DBI->connect("DBI:mysql:s_pradeep:mysql","s_pradeep","54545");

$sth=$dbh->prepare("select * from users order by name");
$sth->execute;
$numrows=$sth->rows;

@col_arr=("white","#cccccc");

print "<table border=1 bordercolor=white style=\"color:#000000\">";
print "<tr bgcolor=aqua><th>ID</th><th>Name</th><th>Email</th><th>Sex</th><th>Date of

Birth</th><th>City</th><th>Country</th><th>Username</th><th>Password</th></tr>"
;

while(@arr=$sth->fetchrow_array)
{
    print "\n<tr bgcolor=\"$col_arr[0]\">\n";
    for($i=0;$i<(@arr-1);$i++)
    {
        print "\t<td>$arr[$i]</td>\n";
    }
    print "</tr>";
    unshift(@col_arr,$col_arr[1]);
    pop(@col_arr);


}

print "</table></body><br>\n\n";
print "<font color=red>Total number of rows affected : <b>$numrows</b></font>";
exit;



All times are GMT +5.5. The time now is 09:45.