1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Connect to MySQL from PHP

How to connect to your MySQL database from a PHP document


You can open a connection to your MySQL database from PHP using the mysql_connect() function. Here is an example:

 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("Database_Name") or die(mysql_error()) ; 
The first variable (shown as your.hostaddress.com) is the location of your database. Often this is "localhost". The next two variables are your username and password. This is the login you use for your MySQL database.

In the second line, we choose which database you are connecting too. This is needed because the same MySQL username can be linked to several databases.

Once you have connected to the database, you can run any SQL code to access and interact with the information in the database.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. PHP Functions
  5. How to Connect to MySQL From a PHP Document

©2014 About.com. All rights reserved.