| Connecting to a MySQL Database Using PHP |
|
|
|
| Saturday, 08 July 2006 | |
|
Here are the steps to connect to MySQL, select a database, and then close the MySQL connection. Complete Code ExampleI start off giving you the complete code example: how to connect to MySQL server, select a database, where to put your code for querying the database, and then how to close the connection to the MySQL server. Following this complete example, I show each step seperately. <?php // *************************************** // Step 1: Connecting to the MySQL server // *************************************** // First, connect to the MySQL server. // We use 'localhost' because the Web server running this PHP script // is on on the same machine as the MySQL server. $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; // *************************************** // Step 2: Selecting Your Database // *************************************** // Select one of the many databases on the server. // Here, we are selecting a database named 'foo'. // Usually other people have databases on the same server // and you need to select your database to work with. // Later in the script, you could do this again // to connect to another database, if you have multiple databases // on the MySQL server. $db_selected = mysql_select_db('foo', $link); if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error()); } // ********************************************** // Step 3: Querying the Database with Your Code // ********************************************** // Enter your wonderful PHP code here // that runs MySQL queries on the MySQL server // you just connected to, using the database you selected // YOUR CODE GOES HERE // ******************************************************* // Last Step: Closing the Connection to the MySQL Server // ******************************************************* // Now always close the connection when you're done. mysql_close($link); ?> Step 1: Connecting to MySQL Using PHPThe first step is connecting to the MySQL server. To connect, you login in using your MySQL username and password. And since you're logging in from a PHP script, you need to tell PHP the URL of the MySQL server. If the MySQL server is on the same machine as the Web server running PHP, then you just use 'localhost' as the URL. The connection parameters depend on your Web host company: they will tell what you need to connect using mysql_connect() from PHP.
<?php // First, connect to the MySQL server. // We use 'localhost' because the Web server running this PHP script // is on on the same machine as the MySQL server. $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> Step 2: Selecting a MySQL DatabaseOnce you have connected (logged in) to MySQL, you will need to select the database you will work. Even if your Web host allows you to have only one database, you will still need to select it. A MySQL installation can have hundreds of databases. You need to tell it which one you want to query. Connecting to MySQL does not imply which database you will work with, even if you have only one database.
<?php // *************************************** // Step 2: Selecting Your Database // *************************************** // Select one of the many databases on the server. // Here, we are selecting a database named 'foo'. // Usually other people have databases on the same server // and you need to select your database to work with. // Later in the script, you could do this again // to connect to another database, if you have multiple databases // on the MySQL server. $db_selected = mysql_select_db('foo', $link); if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error()); } ?> Step 3: Querying the Database with Your CodeI only mention this step, just to be clear. You work with the database after you have:
After connecting to the server and selecting a database, then you can start querying the database and writing to it, etc. <?php // ********************************************** // Step 3: Querying the Database with Your Code // ********************************************** // Enter your wonderful PHP code here // that runs MySQL queries on the MySQL server // you just connected to, using the database you selected mysql_query('YOUR SQL CODE GOES HERE'); ?> I am not going to show any code examples for this part, because that's not the point of this article: to show how to connect to the database. Connecting to the database is a routine process that needs a quick reference, which is what this article is all about.
Last Step: Closing the Connection to the MySQL ServerAfter you have finished working with the database in your script, you need to close the connection. The last step is to close the connection to the MySQL server. You want to do this to free server resources, since other people are using the same MySQL server, which is most likely the case since most people work in a shared hosting environment with many Web hosting accounts on a single server.
<?php // ******************************************************* // Last Step: Closing the Connection to the MySQL Server // ******************************************************* // Now always close the connection when you're done. mysql_close($link); ?> |
|
| Last Updated ( Saturday, 08 July 2006 ) |
| Next > |
|---|
Web Dev Reference 










Everyone needs a short, to-the-point, reference tutorial on how to use PHP to connect to a MySQL database. All PHP/MySQL applications need the same basic code. Here it is.
