Sometimes you may need to connect your PHP driven website to a database. In most instances content management systems this is done through the config.php file. Below is a sample PHP script that connects to a database and shows all the fields for a specific table you specify in the code.

Important! In order for the database connection to work, you will need to create the database, add the database user, and run the script on the server. If you need to run a database script on your local computer, you will need to set up your computer to run Apache, MySQL, and PHP. You can do this by installing WAMP (Windows), MAMP (Mac), or XAMPP.

<?php
 
//Sample Database Connection Script 
 
//Setup connection variables, such as database username
//and password
$hostname="localhost";
$username="your_dbusername";
$password="your_dbpassword";
$dbname="your_dbusername";
$usertable="your_tablename";
$yourfield = "your_field";
 
//Connect to the database
$connection = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $connection);
 
//Setup our query
$query = "SELECT * FROM $usertable";
 
//Run the Query
$result = mysql_query($query);
 
//If the query returned results, loop through
// each result
if($result)
{
	while($row = mysql_fetch_array($result))
	{
		$name = $row["$yourfield"];
		echo "Name: " . $name; 
} } ?>

So let's take a look at the actual code and what you need to replace:

$hostname: This almost always refers to 'localhost' unless you are connecting to an external database.

$username: This is the MySQL user you want to connect with. Keep in mind the user must be assigned to the database.

$password: This is the password for the username you just entered in.

$dbname: This refers to the database name you wish to connect to.

$usertable: This is not needed to connect but in this script it refers to a specific table within the database.

$yourfield: This is not needed to connect to the database but tells the script which field to echo to the screen.

Like this Article?

Login to comment.

Your Opinion Matters

... but we need to know what you're thinking!

I'm Tim Sisson, your friendly Community Support technician, and I wrote the article you're looking at now. I like to think it's perfect, but I'm sure you have some suggestions. Please, let me know what they are!

Feedback
Your Email Address
Because we'd like to talk with you!

We've been listening!

2013-02-13 01:55 pm
Added information about running the script on the server or using WAMP, MAMP, or XAMPP.

Latest Questions

If you need some help, submit your question to our Community!
We guarantee a response within 60 minutes (8am - 9pm EST, Monday - Friday)
Ask a Question!
Recent Questions
  1. Email logs - where shall I find log on who, when, IP address, has open a connection
  2. Creating a Cname
  3. 500 Firewall Error

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!