1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

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

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

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

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.

Subscribe To My YouTube Channel:

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.

include('config.php');

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.

include('key/config.php');

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

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Make a Config.php file to connect to MySql Database

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.