Connecting to MySQL with PHP

An example of connecting to MySQL with PHP.

You can connect to MySQL with hostname “localhost”.

The following example connect’s to database “db3333a” with username “us3333a” and password “your_password”. You will need to substitute these values for your actual database, username, and password you setup in the Control Panel.

$link = mysql_connect('localhost', 'us3333a', 'your_password');

if (!$link) {

   die('Not connected : ' . mysql_error());


// make foo the current db

$db_selected = mysql_select_db('foo', $link);

if (!$db_selected) {

   die ('Can\'t use foo : ' . mysql_error());


Please consult the PHP documentation for more details.

Posted 2008-03-10 in Databases,Web Development