1. Computing

Discuss in my forum

How to connect from a PHP file to a MySQL database

By , About.com Guide

One of the best features of PHP is how easily it interacts with MySQL. This allows you to store information in a database for your website to access and then create dynamic content. In order to do this you need to be able to read and write to the MySQL database from your PHP based website.

You can connect to your database from a PHP file with the following code:

<?php 
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("Database_Name") or die(mysql_error()) ;
?>

Obviously you need to change the highlighted items to represent the actual values for your database.

Once you are connected to your database you can read data or write to your database.

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP with MySQL
  5. Running MySQL commands from PHP

©2013 About.com. All rights reserved.