Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl/Mysql

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
US
Hello,

Does anyone know of any good references that go over connecting to mysql from perl? Its something i haven't done, but need to do for a current project.

THanks,

Aaron
 
I'm very new to this but this should help. It took me a while to hook up to the database!

Code:
#!/usr/bin/perl

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

use DBI;

         $db = 'xxxxxxxxxxdb';
       $host = 'xxx.xxxxxxxxxx.co.uk';
    $db_user = 'xxxxx';
$db_password = 'xxxxx';

##################################################
  
$dbh = DBI->connect("dbi:mysql:$db:$host", "$db_user", "$db_password") || die ("Cannot connect to the database");
            
$sth = $dbh->prepare("SELECT * FROM [red]employees[/red]");

$sth->execute(); if ($sth->err) {die "Error ", $sth->err;}

##################################################

print "<table border='1'>\n";

while (@row = $sth->fetchrow_array) {
  print "  <tr>\n";
  foreach $field (@row) {
    print "    <td>$field</td>\n";
  }
  print "  </tr>\n";
}

print "</table>\n";

##################################################

$sth->finish;
$dbh->disconnect;


Kind Regards
Duncan
 
I think that should do it for me, thanks!
 
Theres an excellent O Reilly book I bought, called "Programming the Perl DBI". Invaluable.
 
If i had the money, i think id get the O'reilly collection
 
A good reference is Programming the Perl DBI book by Descartes and Bunce. ISBN: 1-56592-699-4
 
This is littile advanced to displayed onto a browser when u have a parameter

use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;

my $dbh = DBI->connect( 'dbi:mysql:host=xxxxxxxx; sid=xxxx;port=xxxxx',
'user',
'pass', { RaiseError => 1, AutoCommit => 0}
) || die "Database connection not made: $DBI::errstr\n";

my $start =$q -> param('Num');

if ($start)
{

my $sql_A=qq{ SELECT sname from employee where id = '$start'};

my $sth_A = $dbh->prepare ($sql_A)|| die "Prepare failed: $DBI::errstr\n";
$sth_A->execute() || die "Couldn't execute query:$DBI::errstr\n";


print "Content-Type: text/html\n\n";
print "<html><body>";
print "<BODY leftmargin=0 topmargin=0>";
print "<table height=\"100%\" width = \"100%\" cellpadding = \"0\" cellspacing = \"0\" border = \"2\" align = \"center\">";
print "<tr height=\"5%\">";
print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">";
print "<FONT face=\"Times New Roman\" size=\"5.5\" Color =\"White\"> Result </font>";
print "</td>";
print "</tr>";
print "<tr height=\"90%\">";
print "<td width = \"100%\" colspan = \"2\">";
print "<br>";
print"<br>";
print "<table align=\"center\" bgcolor=\"#E5EEF3\" border=2 bordercolor=\"navy\" >";
print "<tr>";
print"<td><b> Employee Name</b></td>";
while (my @row= $sth_A->fetchrow_array())
{

foreach my $my_td (@row) { print "<td>$my_td</td>\n"; }

}
$sth_A->finish();
print "</tr>\n";
print "</table>";
print "<br>";
$dbh->disconnect() || die "Failed to disconnect\n";
print "<br>";
print "<tr height=\"5%\">";
print "<td align = \"center\" colspan =\"2\" bgColor=\"navy\">&nbsp;</td>";
print "</tr>";
print "</table>";
print "</body></html>";
}

else

{
print "Content-Type: text/html\n\n";
print "<html><body>";
print "<BODY leftmargin=0 topmargin=0>";
print "<table height=\"100%\" width = \"100%\" cellpadding = \"0\" cellspacing = \"0\" border = \"2\" align = \"center\">";
print "<tr height=\"5%\">";
print "<td align = \"center\" colspan =\"2\" bgColor=\"navy\">";
print "<FONT face=\"Arial\" size=\"5.5\" Color =\"White\"> Error </font>";
print "</td>";
print "</tr>";
print "<tr height=\"90%\">";
print "<td width = \"100%\" colspan = \"2\">";
print "<table align=\"center\" bgcolor=\"aliceblue\" border=2 bordercolor=\"navy\" >";
print "<tr>";
print "<td><b>No Data were entered , Please go back and enter data again !! </b></td>";
print "</tr>";
print "</table>";
print "<A href= Go Back </B>";
print "<tr height=\"5%\">";
print "<td align = \"center\" colspan =\"2\" bgColor=\"navy\">&nbsp;</td>";
print "</tr>";
print "</table>";
print "</body></html>";
}


hope it helps when u start working with more advanced level
good luck
ronan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top