Home Guides Glossary

Connecting to a MySQL Database Using CGI/Perl

This example describes using CGI/Perl to connect to a MySQL Database.

#!/usr/bin/perl use cPanelUserConfig;
print "ContentType: text/html\n\n";

use DBI;

# Connecting to the database
# Replace DATABASENAME with the name of the database,
# HOSTNAME with the hostname/ip address of the MySQL server.
$drh = DBI->install_driver("mysql");
$dsn = "DBI:mysql:database=your_databasename;host=mysql.secureserver.net";
$dbh = DBI->connect($dsn,\'your_dbusername\',\'your_dbpassword\');

# Select the data and display to the browser

my $sth = $dbh->prepare("SELECT * FROM customers");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}";
}

$sth->finish();

# Disconnect from the database.

$dbh->disconnect();

Domain Registration

Pay less for website domain names. Register your own .com, .net or .org for as low as $10.18 per year. We have everything you need to get online with your new domain.

Website Builder

Build an amazing website in just under an hour with Website Builder. Take advantage of designs created just for your industry and then customize them to reflect your one-of-a-kind idea.

Website Security

Protect your website and keep customers safe. Your comprehensive Website Security solution. Get peace of mind by securing your websites. We fix hacks to help prevent attacks.

cPanel Hosting

Everything needed to give your website the high-performance home it deserves.  Protect transactions and secure your customer's data with a SSL Certificate
Copyright © 2005 - 2022. All rights reserved. Privacy Policy