|
"My Perl script works on my server but it does not work on
your server."
MySQL Connection
Sometimes, you may be wondering how to make your Perl
script connect to the database that you have just got from us. By
default, we set up your database in the same server as the web server.
In addition, you might receive information like this from us:
===========================================================
MySQL ACCOUNT LOGIN INFO
===========================================================
Host : localhost
Database name : mydatabase_db
Username : mydbuser
Password : mydbpass
===========================================================
There are many ways to connect to the database. Here is
just one example. The below is a simple example of how you can connect
to your database, using the information we gave you above, do a simple
query, and display the results in a table.
#!/usr/local/bin/perl
use DBI;
print <<END;
Content-type: text/html
<html>
<head>
<title>Example of Perl calling MySQL</title>
</head>
<body bgcolor="white">
END
# database information
$db="mydatabase_db";
$host="localhost";
$userid="mydbuser";
$passwd="mydbpass";
$connectionInfo="dbi:mysql:$db;$host";
# make connection to database
$dbh = DBI->connect($connectionInfo,$userid,$passwd);
# prepare and execute query
$query = "SELECT * FROM mytable WHERE Age > 30 ORDER BY Name";
$sth = $dbh->prepare($query);
$sth->execute();
# assign fields to variables
$sth->bind_columns(\$ID, \$Name, \$Age);
# output name list to the browser
print "Names in the mytable:<p>\n";
print "<table>\n";
while($sth->fetch()) {
print "<tr><td>$Name<td>$Age\n";
}
print "</table>\n";
print "</body>\n";
print "</html>\n";
$sth->finish();
# disconnect from database
$dbh->disconnect
|