Fix Error Establishing a Database Connection in WordPress

Recently i was trying to install WordPress on my friend’s localhost but an error occurred which said – “Error establishing a database connection in WordPress“.

I’ll not go in detail, lets just solve the error right away. In order to fix – Error establishing a database connection, you’ll need to follow two steps.

  1. Creating a new Database
  2. Creating Database User
  3. Editing wp-config file of your WordPress package.

Where to Find wp-config file?

If you plan to install WordPress on Local Host using WAMP server then wp-config file has to be inside www folder (C:wampwww). If you plan to install WordPress on Web Host then you can access wp-config file using FTP Client like FileZilla.

You need to know the right location of wp-config file in order to proceed. If at all you have any difficulty to find wp-config, just leave a comment below and i’ll surely help you out.

Step 1 – Creating Database

Step 2 – Creating Database User

  • If you are installing WordPress on Local Host then just go to step 3
  • If you are installing WordPress on Web Host then read THIS article.

Step 3 – Editing wp-config file

When you open wp-config file, you’ll see something as shown below.

// ** MySQL settings - You can get this info from your web host ** //
 /** The name of the database for WordPress */
 define('DB_NAME', 'DATABASE_NAME_HERE');

/** MySQL database username */
 define('DB_USER', 'root');

/** MySQL database password */
 define('DB_PASSWORD', '');

/** MySQL hostname */
 define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
 define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
 define('DB_COLLATE', '');
WP Local Host Installation

If you have created database named XYZ then make the following changes in your wp-config file –

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'XYZ');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', '');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

WP Web Host Installation

If you have created database named ABC and created a user with username: admin and password: hello then just make the following changes in your wp-config file (which is on the server).

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'ABC');

/** MySQL database username */
define('DB_USER', 'admin');

/** MySQL database password */
define('DB_PASSWORD', 'hello');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

Long Story Short

You need to create database and database user. The database user will have username and password. This error usually occurs because of mismatch in configuration. So to fix it, open wp-config file and enter the right database name, database username and database user password. Save the file where it is supposed to be. And problem of “Error establishing a database connection in WordPress” will be solved.

Thanks for reading and do subscribe to blog feed for more amazing posts. You can find the subscription box below.

Custom Tag Buttons Widget For WordPress Using CSS

I’ll be sharing the code for custom tag buttons which you can use as a widget in your WordPress Blog. I have designed these buttons using CSS.

So in order to implement these, you’ll need to add two part of codes. One is CSS and other is HTML. You’ll need to put the CSS code at the bottom of style.css file of your WordPress theme. And you’ll have to add the HTML code inside a text widget.

Below is the screenshot of how the custom tag buttons look like. These are very simple buttons with round border. Also when you move your cursor over the button, the color of the button changes into a darker shade.

Custom Tags

CSS Code


a.tagbutton {
background: #333;
text-transform: uppercase;
color: #fff;
font-size: 10px;
font-family: arial,sans-serif;
text-decoration: none;
opacity: .9;
padding: 4px 10px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
-webkit-transition: background 200ms linear;
-moz-transition: background 200ms linear;
transition: background 200ms linear;
-webkit-backface-visibility: hidden;
margin: 0 5px 5px 0;
display: block;
float: left;
}

a.tagbutton:hover {
background: #000;
}

You can use the CSS code as it is. But if you want to make any changes, feel free to experiment with it. If you are not sure about how to use it, then follow these steps –

  1. Open WordPress Administrators Dashboard
  2. Go to Appearance > Editor
  3. This should open the Stylesheet (style.css)
  4. Go to the bottom of this file and paste the CSS code given above.
  5. Save the file.

HTML Code

The HTML code which i use to display the buttons in the screenshot is given below. Note that, these buttons link to my blog categories.


<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/how-to/">How-to </a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/google/">Google</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/android/">Android</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/windows/">Windows</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/wordpresstricks/">WordPress</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/bloggertricks/">Blogger</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/optimize/">Optimize</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/monetize/">Monetize</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/gaming/">Gaming</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/tag/windows-8/">Windows 8</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/web-hosting/">Web Hosting</a>
<a class="tagbutton" style="color:#fff" href="http://www.codefap.com/category/photoshop-elements-10/">Photoshop Elements 10</a>

You cannot use this code as it is. You’ll need to change it according to your need. For example, if you need a custom tag button to link to google.com then the format for that is given below –


<a style="color:#fff" href="http://www.google.com/">Google</a>

In this way you can write a html code for a button with desired link. And then just copy the html code and use it inside a text widget. If you are unsure about how to use the code then follow these steps –

  1. Open WordPress Administrators Dashboard
  2. Go to Appearance > Widgets
  3. Drag the text widget into your desired Widget Area
  4. Paste the html code inside the content area of Widget. (leave the title empty)
  5. Save the widget.
  6. Go to your blog’s home page and you’ll find the custom tag button in the widget area of your choice.

Thanks for reading and do subscribe to blog feed for more amazing posts. You can find the subscription box below.

Installing WordPress On Subdomain Using cPanel

In this post, I’ll show how to install WordPress on a subdomain using the softaculous installer in cPanel of Linux Web Hosting. This is very simple, you just have to follow the steps given below.

I assume you already have a hosting account with a domain associated to it. So just log into your web hosting account and open the Control Panel (cPanel) of Linux hosting. Some hosts hide cPanel under the name ‘Manage Web Hosting‘.

Creating Sub-Domain

Once you open cPanel, navigate or scroll down to the ‘Domains‘ section. Here select the subdomains option and a new page will open up.

You can straightaway figure out how to create a subdomain, but if you can’t then keep reading. On the subdomains page, you can find ‘Create a subdomain‘ section. Check the screenshot given below –

All you need to do is just enter the subdomain name and select the domain. In this case, I have chosen the domain as codefap.com and the subdomain as lab. And do not mess with the document root, it will be automatically created once you enter the subdomain.

After entering subdomain and domain in the form, just hit Create button. In this case, a subdomain with link lab.codefap.com will be created for me.

Installing WordPress On Sub-Domain

Installation of WordPress package on a sub-domain is very similar to that of installing it on root itself. All you need to do is just select the newly created subdomain from the drop down menu.

Install WordPress On SubDomain

Fill the rest of the details as described in my previous post – Installing WordPress On The Web Host. I hope this post helps you. Subscribe for more amazing posts, Thanks.