Frequently Asked Questions

Connecting to MySQL Using PHP

Last Updated: February 14, 2007 10:42 AM

There are two main steps to connecting to a MySQL database using PHP.

First, connect to your MySQL server using the mysql_connect statement. For example:

mysql_connect('HOSTNAME','USERNAME','PASSWORD');

You can log in to your hosting account manager to find the hostname, user name, and password for you database.

Next, select the database that you want to access using mysql_select_db. For example:

mysql_select_db('DATABASENAME')

Where "DATABASENAME" is the name of your database. Again, you can log in to your hosting account manager to find the name of your database. The name of your database is usually the same as the user name for that database.

From here, you can query your database using your PHP script.

For more information, take a look at our Complete PHP/MySQL Example.