"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:

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.


use DBI;

print <<END;
Content-type: text/html

<title>Example of Perl calling MySQL</title>

<body bgcolor="white">


# database information

# 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);

# 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";


# disconnect from database