To connect to a Mysql database you need a few things first.
Of course you need to have the database created with a name you make up as well as the username and password of the mysql account that can connect to that database. This is very simple if you are using web managing software such as Cpanel or DirectAdmin. Under the Mysql databases area you can do all of this quickly.
For many websites mysql connections are included in a common file such as “config.php”. Here is how a basic mysql connection looks:
<?php $host = "localhost"; $db_username = "USERNAME"; $db_password = "PASSWORD"; $db_name = "DATABASE"; mysql_connect($host,$db_username,$db_password) or die(mysql_error()); // should there be any errors, we will receive them before reaching any further // we have connected to the mysql server successfully, now let's get into the database mysql_select_db($db_name) or die(mysql_error()); // if the following returns no errors, you are now connected to your mysql database ?>
$host – The host that you are connecting to. Usually set to “localhost”.
$db_username – The username of the account that connects to the database specified.
$db_password – The password of the account that connects to the database specified.
$db_name – The name of the database you are connecting to. This is shown in your domain managing area.
This code will connect to a mysql database and if there are any errors reported it will end php and display the errors.