Home - Help & Support - Contact Us
    

Connecting To A MySQL Database Using CGI/Perl

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

#!/usr/bin/perl -T
print "ContentType: text/htmlnn";

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'}n";
}

$sth->finish();

# Disconnect from the database.

$dbh->disconnect();
Rated:NR/0 Votes
373 Views
More Articles From CGI
More Articles From CGI
Print Article
Print
Share |
Comments, Questions & Reviews

Comments is currently disabled for this article!
Related Articles and Readings
Connecting to a MySQL Database on Your Linux Hosting
This example describes using PHP to connect to a MySQL Database on a Linux hosting account. <?php//Sample Database Connection Syntax for ...
Connecting to a MySQL Database on Your Linux Hosting Account Using JSP
This example describes using JSP to connect to a MySQL Database on a Linux hosting account. <%@ page contentType="text/html; charset=iso-8859-1" language="java"import="java.sql.*" ...
What are the Methods for Backing up my MySQL Database?
There are two common methods for backing up the content in your MySQL database. Dump A copy of the database in ASCII(text) ...
Setting up a MySQL Database for Your Web Site
MySQL is a database server that you can use in conjunction with PHP and other scripting languages to create dynamic ...
Setting up a MySQL Database on Your Windows Hosting Account
MySQL is a database server that you can use in conjunction with PHP, ASP, and other scripting languages to create ...
The information provided in this article is for general information and/or the comments is the sole responsibility of their respective authors and does not necessarily reflect the opinion of zeronese.net does not endorse any article and/or comments published by our web users unless otherwise noted.