Connecting to MySQL Using PHP
Print this Article
Comment on this Article
Last Updated:
April 8, 2008 2:03 PM
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:
$con = mysql_connect('HOSTNAME','USERNAME','PASSWORD');
You can log in to your hosting account manager to find the hostname, user name, and password for your database.
Next, select the database that you want to access using mysql_select_db
. For example:
mysql_select_db('DATABASENAME', $con)
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.