1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MySQL-Perl Connectivity

Discussion in 'Perl' started by pradeep, Apr 20, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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:
    #!/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;
     

Share This Page