InMotion Hosting Support Center

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 be sure that you attach a MySQL user to the database 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 (Windows), MAMP (Mac), or XAMPP.

//Sample Database Connection Script 
//Setup connection variables, such as database username
//and password
$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
  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.

Support Center Login

Social Media Login

Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
How do I connect to a MySQL Database with PHP?
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-04-14 10:36 am




connect mysql exactly

define ("DB_HOST", "localhost");

define ("DB_POST", "");

define ("DB_USER", "root");

define ("DB_PASSWORD", "");

define ("DB_NAME", "");




class Db {

      static $connection  = NULL;

public function __constructe (){


    if (!isset(self::$connection)){

    self::$connection = mysql_connect (DB_HOST.":" . DB_POST, DB_USER, DB_PASSWORD);

    mysql_select_db( DB_NAME , self::$connection);

    mysql_query ( " SET CHARACTER SET utf8 ") or die (mysql_error());






public function qurery ($query){

        $data = mysql_query($query,self::$connection);

        if (is_bool($data)) return array;

        $rows = array();

        while ($row = mysql_fetch_assoc($data)){

        $rows[] = $row;


        return $rows;










class Result extends {

     private $_table = "name_table";

     public function __construct(){



     public function select (){

     $query = " SELECT * FROM " . $this->_table;

     $select_table = $this->query($query);

     return $select_table;









     $data = new Result();

     $select = $data->select();


11,186 Points
2014-04-14 10:45 am
Your question is a bit unclear. Could you clarify what we can assist you with?
n/a Points
2014-04-16 10:04 am

I want to ask you, is there a standard way to connect database and correct, 

I see a lot of connection types and display data, the standard way with php and mysql not, ?

 as I have presented is called the standard php mysql not ?

Best Regards,

26,137 Points
2014-04-16 12:08 pm
Hello Viet nam,

If you are hosting on our servers, then database is "local" to the website files, so the connection is made "locally". Therefore, the connection setting is set to "localhost" by default. If you're trying to connect to a database outside of the hosting server, then you would have to setup a connection string using something like the mysqli_connect() function .

I hope that helps to answer your question. Please let us know if you need further assistance.

Arnel C.
n/a Points
2014-11-07 8:31 am

Hi sir i am Guna,

I am new to PHP. i know the Concepts of Core PHP. but i have no idea about PHP mailer. concepts so please help me for that like how to send a mail using PHP. i have the files(class.pop3.php, class.phpmailer.php & class.smtp.php) but i dont know how to use that and configure. so please help me.




28,948 Points
2014-11-07 12:14 pm
Hello Guna,

We do not have specific articles on those particular functions, however we do have an article on php mailer that may help.

Kindest Regards,
Scott M
n/a Points
2015-08-14 9:40 am

hi sir. i ask if that code is the same in the storing data in the data base??? and that is the code for MYSQL . how about for the sybase ?? they are the same ?? 


26,137 Points
2015-08-14 3:08 pm
Hello Arvin,

Thanks for the question. I would hesitate to say it's the same in Sybase, the underlying principles may be the same, but the code is different. We do not provide coding support as it is beyond the scope of our support.

If you have any further questions or comments, please let us know.

Arnel C.
n/a Points
2015-08-19 12:11 pm

where do i upload the config.php script using ftp?

26,137 Points
2015-08-19 1:36 pm
Hello Michael,

We're not exactly sure what you're using, or if you're hosting a site with us. The location may differ depending on the software you're talking about, your host, and where you have installed the website. Please provide more information about your installation - website software used, how it was installed, and where it was installed.

If you have any further questions or comments, please let us know.

Arnel C.

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

10 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


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:
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!