Select Category
Sitemap Help Contact
print article

Connect to Your MySQL Database using PHP

For webhosting packages.

Follow the steps below to establish a connection to your MySQL database using PHP.

If you have a managed server, follow the steps to Connect to a MySQL5 Database on a 1&1 Managed Server instead.

To be able to connect to a MySQL database, you will need the following MySQL Database Connection Credentials:

  • Database Name
  • Host Name
  • User Name
  • Password

Copy the script shown below into a .php file, replacing the hostname, database, username, and password with your Database Name and your password, etc., then upload the file to your webspace. This script is only for connecting to MySQL. Additional commands will need to be added manually.

<?php

//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="db1111111111.db.1and1.com";
$database="db1111111111";
$username="dbo111111111";
$password="myPassword";

//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
     echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}

$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
    die ('Can\'t select database: ' . mysql_error());
}
else {
    echo 'Database ' . $database . ' successfully selected!';
}

mysql_close($link);

?>

For additional information, you may want to reference: