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,

30,062 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.




33,905 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 ?? 


30,062 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?

30,062 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.
n/a Points
2015-09-11 9:37 am


I am working for a client with a VPS on inmotion. The work I'm doing is being moved from my server to the client's. I have created the database, database user, attached the database user to the database and imported structure and data (verified using phpMyAdmin).

However, the PHP code is unable to connect to the database. I'm using "localhost" (which is what phpMyAdmin shows), I've double-checked and double-set the password, ensured that username and database names are correct (including prefix with underscore). I've even given the database user all privileges for the purpose of troubleshooting.

MySQL Info:

  • Server: Localhost via UNIX socket
  • Server type: MySQL
  • Server version: 5.1.73-cll - MySQL Community Server (GPLv2)
  • Protocol version: 10
  • Server charset: UTF-8 Unicode (utf8)

PHP Version 5.4.45, using mysqli (and have verified through phpinfo() that mysqli is supported in the build).

What assistance can you offer?




33,905 Points
2015-09-14 12:41 pm
Hello Keith,

From what you told me, there shouldn't be any issues. You may want to contact our Live Support so wec an walk through the setup step by step. It would require divulging some sensitive info you do not want to put here.

Kindest Regards,
Scott M
n/a Points
2015-10-01 1:09 pm

hi! im new to PHP but im willing to learn. we are required by oir professor to develop an "Online grading system".. we already have our database for students' login account (containg their username & password).. our index and login page is already okay.. but our problem is, how will I make that when a student logged in, for example me (via karen), when i logged in , my grades for the semester will appear. and when my classmate (Mark) logged in in his account, his grades na ssubjects for the semester will appear.? can you help us please?



21,102 Points
2015-10-01 2:37 pm
Hello Karen,

Thank you for contacting. We are happy to help guide you, but your question will require a custom-coded solution.

I recommend consulting a developer to assist you in working toward a solution.

Thank you,
n/a Points
2015-11-20 3:54 pm

Not clear about the topic above. My question is, on migrating to inmotion, what exactly do i replace "localhost" with? It's confusing what should be used.

30,062 Points
2015-11-20 6:10 pm
Hello Olanrewaju,

The problem here is that we quite often address people not being hosted with InMotion Hosting. So, if you're not a customer and you don't understand how to connect to your database, then you'll need to speak with your host's technical support, or speak with a local developer who can look at your code. For our customers, you do NOT replace "localhost" because the web server connects to the database locally in our network. The article above was created for InMotion Hosting customers in general. However, we do try to help anyone who asks, but we would need more information about how you are connecting to your database server in order to provide you an answer.

I hope this helps to answer your question, please let us know if you require any further assistance.

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.

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