Mysql Database Connect

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:

$host = "localhost";
$db_username = "USERNAME";
$db_password = "PASSWORD";
$db_name = "DATABASE";

mysql_connect($host,$db_username,$db_password) or die(mysql_error());

$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.

0

Popularity: 1% [?]

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Filed Under: ScriptsTutorialsWeb Programming

Tags:

RSSComments (1)

Leave a Reply | Trackback URL

  1. [...] Before we can grab anything from a mysql database we have to connect to it. Find how to connect to a mysql database here. [...]

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

The tutorials and scripts found on bgallz.org are for training purposes only, use to your discretion.