Make a Config file to connect to MySql Database

In order to connect to a database you need to make a key file. Make a Config file to connect to MySql Database. This is a fairly simple process. You take the information that you assigned to a database when you granted privileges to it and make a  file and call it config.php. You can call the file whatever you want what is important is that you have the right login information to the database. Some people call their key file common.php or key.php.

Here is the code that you will need for our examples that we will be doing in php. Put this code in your config.php file that you created with your FTP program..

[code]$con = mysqli_connect("localhost","dbusername","dbpass","dbname");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "";
}
[/code]

This is all you have to do to create a config file so that you can connect to a database. If you need to learn more about setting php variables and how they work please see PHP Variable

Only cool people share!

When you make a config file to connect to MySql database you need to remember that this will be the key file that you will include on all your pages that you want dynamic content on. Dynamic content is different than static content in the sense that you are dynamically inserting content on a page from a database.

How to include the config file on pages.

After you make a config file to connect to MySql database you will need the following code to include on your dynamic pages.

[code]include(‘config.php’);[/code]

Now if you put your config file in a different directory or folder you would have to make sure that your path reflects it. In the example below we have a folder called key that we put our config file in.

[code]include(‘key/config.php’);[/code]

Be sure that you are aware of file paths, most of the time this is what gives newbies to PHP trouble because they confuse the URL path.

Video Instructions

Make a Config file to connect to MySql Database was last modified: October 18th, 2015 by Maximus Mccullough
Summary
Make a Config file to connect to MySql Database
Article Name
Make a Config file to connect to MySql Database
Description
In order to connect to a database you need to make a key file. Make a Config file to connect to MySql Database. This is a fairly simple process.
Author
Make a Config.php file to connect to MySql Database