1. Home
  2. Computing & Technology
  3. PHP / MySQL

Connect to MySQL from PHP
How to connect to your MySQL database from a PHP document

By Angela Bradley, About.com

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

<?php
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.

More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. Connect to MySQL from PHP - Use PHP with MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.