The community is working on translating this tutorial into Spanish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

PHP and MySQL:

Establishing a connection

In the previous chapter, we created a database table with some test data. Now we need to establish a connection to the database server so that we can start working with the data. Connecting to a MySQL database with PHP is very easy. It can be done using a single function from the nice array of MySQL related functions in PHP:

mysql_connect("localhost", "username", "password");

If default values have been configured in your configuration file, you can leave out these parameters, but otherwise you will need to specify a host and typically a username and a password for it. Both the username and password has been set by yourself or by your hosting company. If you host MySQL on your own machine, or if you run your PHP code on your hosting company's server, you can usually just specify localhost as the host value. If in doubt, ask your hosting company or check their support pages.

The mysql_connect() function returns a resource, a direct link to the database server, which should be used to access the database server each time you use one of the MySQL functions. However, if this resource is not specified, PHP will just use the last opened connection, allowing you to write less code. In most cases you will only need one MySQL connection per page, so this should work just fine for you.

To work with a database, you need to call one more function, the mysql_select_db() function. The name really tells it all - for a specific connection, it selects a database that you will be working with. It's very simple to use:


This should be called after you have established the connection using mysql_connect(). It takes one or two parameters. The first should be the name of the database you wish to use. The second one is optional and allows you to specify which MySQL resource link the function should be performed on. Here is an example where we use these two essential functions together:

mysql_connect("localhost", "username", "password");

And here is the same example, but where we get the resource link from mysql_connect() and use it. This is the way you should do it mainly if you need to connect to more than one database server on the same page:

$dbConnection = mysql_connect("localhost", "username", "password");
mysql_select_db("my_database", $dbConnection);

With this, we now have a connection to the database server and we have selected the desired database. In the next chapter, we will use this connection, but before we do so, we should talk a bit about closing the connection again. A connection to a database server is costly, so it should obviously be closed once we're done using it. However, PHP can and will do this for us automatically, if we choose not to do it, once the page is fully executed. If we for some reason want to close a MySQL connection before the page is done executing, we can do it by using the mysql_close() function:


This will close the last opened connection. If we want to close a specific connection, just pass its link to the function:


Okay, with a good understanding of how a connection is started, we can now move on to using it. Read on.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!