How do I connect to my MySQL database?

Before you can use MySQL, you need to create a database for yourself on your Control Panel.

To connect from a PHP script, just put this in your file:

mysql_connect(“MYSQLSERVERNAME”, “DBUSERNAME”, “DBPASSWORD”);
mysql_select_db(“DBNAME”);
?>

To connect from a perl script, put this in your file:

#!/usr/bin/perl
use DBI;

$database = “DBNAME”;
$hostname = “mysql.hosting.ca”;
$port = “3306”;
$username = “DBUSERNAME”;
$password = ‘DBPASSWORD’;

$dsn = “DBI:mysql:database=$database;host=$hostname;port=$port”;

$dbh = DBI->connect($dsn, $username, $password) or die(“Could not connect!”);

$sql = “SELECT * FROM mytable”;

$sth = $dbh->prepare($sql);
$sth->execute;

while(($column1, $column2) = $sth->fetchrow_array)
{
print “C1 = $column1, C2 = $column2\n”;
}

$dbh->disconnect;

where MYSQLSERVERNAME, DBNAME, DBUSERNAME, and DBPASSWORD are your database name, database username and database password.

Tags: ,