---
title: "Connecting to a Database using PHP"
description: "Selecting a reputable web hosting company is only the first step towards building and maintaining a successful website. Sometimes you may need to connect your PHP driven website to a database. For..."
url: https://www.inmotionhosting.com/support/website/connecting-to-a-database-using-php/
date: 2011-12-13
modified: 2023-08-15
author: "Carrie Smaha"
categories: ["Website", "Working with Databases"]
type: post
lang: en
---

# Connecting to a Database using PHP

Selecting a reputable [web hosting company](https://www.inmotionhosting.com/) is only the first step towards building and maintaining a successful website. Sometimes you may need to **connect your PHP driven website to a database**. For most 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](/support/), [add the database user](/support/edu/cpanel/create-database-user/), and be sure that you [attach a MySQL user to the database](/support/edu/cpanel/create-database-user/#assign-user-to-db) before attempting to 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](https://www.wampserver.com/en/) (Windows), [MAMP](https://www.mamp.info/en/index.html) (Mac), or [XAMPP](https://www.apachefriends.org/en/xampp.html).

## How to Connect to a Database Using PHP

<?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;
} }

?>

**NOTE:** cPanel accounts using PHP 7 or higher would need to use *mysqli* instead of *mysql* –

e.g. *$connection = mysqli_connect($hostname, $username, $password);*

You can [find your PHP version in cPanel](/support/website/how-to-change-the-php-version-your-account-uses/) or a [phpinfo page](/support/website/find-php-version-and-options-of-your-server/).

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.

**$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.

With reliable [PHP web hosting](https://www.inmotionhosting.com/php-hosting), you can effortlessly create and maintain dynamic websites and applications that interact with databases efficiently.

 
