How To Connect Database in WordPress Plugin – The Beginner’s Guide

08 Apr, 2019

If you are WordPress codder and trying to develop a custom built plugin to extend the WordPress functionally and facing database connectivity, then this article really will help for you, here I would explain about WordPress database connection with two different database object.

How To Connect Database in WordPress Plugin for MySql?

To make a connection with the WordPress database first we need to ensure we have properly included the WordPress config files which exist into the WordPress root directory.

Step to connect with the database to MySQL:

  • First, include wp-config.php file into WordPress plugin pages
  • Make sure wp-config has included successfully
  • Now use mysql_connect, mysql_select_db function use to create the connection with WordPress database
  •  Require_once( ABSPATH . ‘wp-config.php’);
  • mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    mysql_select_db(DB_NAME);

How To Connect Database in WordPress Plugin for MySqli?

MySqli is the latest version of the database which is based on object-oriented programming which is far more better than MySQL and fully optimized, so by the time start learning mySqli because since later every web application would start using MySqli instead of MySQL.

Steps Connect Database in WordPress Plugin for MySqli :

  • Include require_once( ABSPATH . ‘wp-config.php’);
  • mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  • Successfully connect with wordpress database

Anything else drop a comment please!

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here