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

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.
More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. PHP MySQL Connect - MySQL Connect File PHP - Include MySQL Connect PHP

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

All rights reserved.