Search  
   
Browse by Category
 
Netfirms 24/7 Support .: DATABASE MANAGEMENT .: How do I connect to a mySQL database using Perl?

How do I connect to a mySQL database using Perl?

You may use Perl and the DBI Perl Module to access your mySQL database. See below for a sample perl script that connects to a MySQL database, runs a query, and then disconnects:

#!/usr/bin/perl
print "Content-type: text/html nn";
use DBI;

# Connect To Database
$database = "your database name";
$username = "your database username";
$password = "your database password";
$hostname = "mysqlhost";
$db = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);

if(!$db = DBI->connect("DBI:mysql:$database:$hostname", $username, $password))
     print "Connection unsuccessful. Please check your login credentials. ".$DBI::errstr;
# Execute a Query
$query = $db->prepare("SELECT * FROM test");
$query->execute;

# How many rows in result?
$numrows = $query->rows;

# Display Results
while (@array = $query->fetchrow_array) {
($field1, $field2, $field3) = @array;
print "field1 = $field1, field2 = $field2, field3 = $field3
";
}

# Cleaning Up
$query->finish;
$db->disconnect;

exit(0);

NOTE: The mysql password should be no more than 8 alpha-numeric characters. To identify the database username, password and dbid, click here for further instructions.

For remote connections:

 #!/usr/bin/perl
print "Content-type: text/html \n\n";
use DBI;

# Connect To Database
$database = "your database name";
$username = "your database username";
$password = "your database password";
$hostname = "mysqlhost.netfirms.com:3306";
$db = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);

# Execute a Query
$query = $db->prepare("SELECT * FROM test");
$query->execute;

# How many rows in result?
$numrows = $query->rows;

# Display Results
while (@array = $query->fetchrow_array) {
($field1, $field2, $field3) = @array;
print "field1 = $field1, field2 = $field2, field3 = $field3
";
}

# Cleaning Up
$query->finish;
$db->disconnect;

exit(0);

For remote connections where the database is residing on a different/separate Netfirms hosting account:

A. Identify the NF_MYSQL_HOST address for the account where the database resides:

1. Login to the Netfirms Members Control Panel at https://controlpanel.netfirms.com
2. Click Site Tools
3. Click Environment
4. Refer to the NF_MYSQL_HOST line entry and jot down the value indicate (eg. It will look similar to: NF_MYSQL_HOST=10.8.12.2:3306 but the IP address may be different for your acocunt)

B. Code your PHP script on the account that needs to make the remote database connection

1. The beginning of your PERL script that will make a remote connection to your database residing on Netfirms should contain the following similar line of code:

$ENV{'PATH'} = 'NF_MYSQL_HOST=10.8.12.2:3306';

Below is a sample code:

#!/usr/bin/perl
print "content-type: text/html \n\n";
use DBI;

$ENV{'PATH'} = 'NF_MYSQL_HOST=10.8.12.2:3306';

# Connect To Database
$database = "your database name";
$username = "your database username";
$password = "your database password";
$hostname = "mysqlhost";
$db = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);

# Execute a Query
$query = $db->prepare("SELECT * FROM customers");
$query->execute;

# How many rows in result?
$numrows = $query->rows;

# Display Results
while (@array = $query->fetchrow_array) {
($field1, $field2, $field3) = @array;
print "field1 = $field1, field2 = $field2, field3 = $field3
";
}

# Cleaning Up
$query->finish;
$db->disconnect;

exit(0);



How helpful was this article to you?


.: Powered by Lore 1.5.6
Visit Netfirms.com Web Hosting | Copyright © 1998 - 2006 Netfirms, Inc. All Rights Reserved.