1. Computing
Send to a Friend via Email

Connect to MySQL from PHP

How to connect to your MySQL database from a PHP document

By

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.

Related Video
Basic PHP Syntax
Using PHP With HTML

©2014 About.com. All rights reserved.