1. Computing & Technology

Discuss in my forum

MySQL Connect Files in PHP

Store Your MySQL Connection Code in One PHP File to Reuse

By , About.com Guide

Your website can have several PHP pages that all access your MySQL database. One time saving idea is to store your connection code in a separate file, and then include the file where needed.

For example, we can use the SQL code below to login to our MySQL database. Let's save this code in a file called datalogin.php.

 <?php 
 // Connects to Our Database 
 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 
 mysql_select_db("Database_Name") or die(mysql_error()); 
 ?> 
Now, whenever we need to connect to our database, we just include this line in our PHP file:
 //MySQL Database Connect
 include 'datalogin.php'; 
You can learn more about including files, and connecting to MySQL from PHP in the other tutorials on our website.

©2012 About.com. All rights reserved.

A part of The New York Times Company.