
If you are beginning to work with php and mysql, you may have wondered how to create a connection to a database you can reuse. The best method is to create a file with the database connection function within it, and include it in any page you wish to have database access. This saves you from duplicate code, with less chances of error. It is also rather easy to set up, so that’s get going.
First, open a blank page in your favorite web authoring program. Make sure there is none of the typical code dreamweaver and such like to put into blank documents. Next, copy the code below into the blank page:
<?php
//db.php - Connection String to Database
$dbhost = "localhost"; //Host Name (Localhost is often default)
$dbuser = "XXXXXXX"; //Database Username
$dbpass = "YYYYYYY"; //Username Password
function dbConnect($db="") {
global $dbhost, $dbuser, $dbpass;
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) //Connect to Database
or die("Database is undergoing maintance at this time. Please try again at a later date."); //If Server is not found, display error.
if ($db!="" and !@mysql_select_db($db))
die("Database is undergoing maintance at this time. Please try again at a later date."); //If Database is not found, display error.
return $dbcnx; //End Function
}
?>
That’s break this down line by line. The first line begins the php code. The next line is a comment that tells the programmer (that’s you) what the file is for. Any code you see with // in front of it is a comment, and is for you so you can understand the code.
$dbhost = "localhost"; //Host Name (Localhost is often default)
This sets the variable, $dbhost, to the value of ‘localhost’. If your web host has a specific host name that is required, put that in the place of localhost.
$dbuser = "XXXXXXX"; //Database Username
This sets the variable, $dbuser, to the value of ‘XXXXXXX”. Replace XXXXXXX with the username of your database.
$dbpass = "YYYYYYY"; //Username Password
This sets the variable, $dbpass, to the value of “YYYYYYY”. Replace YYYYYYY with the password for your database.
function dbConnect($db="") {
This is the beginning of your function, named dbConnect. $db is the name of the variable you will use for your database’s name. This is used when calling the function. You will see later on how this is used.
global $dbhost, $dbuser, $dbpass;
These are the variables the function will use to connect to the database. They are the same as the ones above.
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) //Connect to Database
The variable, $dbcnx, will be used to connect to the database with the values entered above.
or die("Database is undergoing maintance at this time. Please try again at a later date."); //If Server is not found, display error.
If the connection fails, display the error message: “Database is undergoing maintance at this time. Please try again at a later date”.
if ($db!="" and !@mysql_select_db($db))
Check to see if the database you provided exists
die("Database is undergoing maintance at this time. Please try again at a later date."); //If Database is not found, display error.
Display error message, “Database is undergoing maintance at this time. Please try again at a later date.”
return $dbcnx; //End Function
End the function by returning the value of the variable to the user. The user only sees the value of the variable if there is an error message.
The last two lines end the function and close the php code.
Now, create a new directory on your server called Connections. Put this directory in the root folder. Now, save the file as db.php and save it in the connections folder. Now you have your connection string in it’s own file, away from your other code. This way you can keep things nice and neat.
Now, in another file you want to use to connect to the database, put this in the top of the page:
<?php require_once('Connections/db.php'); ?>
This allows the page your on to have access to the contents of the db.php file. This is a requirement so your page knows where to get the code. It is always best to do this at the beginning of your page. Now, put in the following code in the page where you wish to access the database:
dbConnect("Database_Name"); //Connect to Database
Replace ‘Database_Name’ with the name of your database. Now, you will have a working connection without having all the code in your page! What is even better is you can call many different databases with the same function, just change the name. Do note, the databases must be on the same server, with the same usernames and passwords.
Play around with the function, you may even be able to get it so you can call many different databases with different usernames, passwords, and hosts.
Related Posts
- PHPBB3 – Private Messaging Error
- Remove Old Links from Google
- Blocking Search Engine Bots
- How to Establish Yourself Online – Part 3: Setting up WordPress
- Example robots.txt for WordPress and PHPBB3
- How to Establish Yourself Online – Part 5: WordPress SEO
- WordPress Attack: Upgrade Now
- Slmgr.vbs –rearm Error
- uTorrent: Disk Overload 100% Error Fix
- How to Establish Yourself Online – Part 4: Customizing WordPress
Tags: connection, mysql, php







