The Must Have for WordPress
The Must Have for WordPress
×
  • Home
December 5, 2019
Database PHP

Create a connection to WordPress MySQL database in PHP

by Martin Jost
No comments
1 min Reading Time
732 Views
5 Score
2 Likes
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp

WordPress stores all the website data in a database and uses it, for example, if a certain content or settings of a post will output to a visitor. In most cases, a MySQL database is used.

As theme or plugin developers there may be reasons to change specific settings in the WordPress database. The following example shows how to connect to the assigned MySQL database.

How can I connect to the WordPress MySQL database?

The following PHP function uses the WordPress constants “DB_HOST“, “DB_USER“, “DB_PASSWORD” and “DB_NAME“, which are already defined in the “wp-config.php” file of WordPress.

How do we use this function?

Since we were able to successfully connect to the MySQL database, we can now use the PHP function above to setup certain changes to the WordPress database.

The following small snippet shows an example of how we can rename an old MySQL table name to a new one.

Last updated: December 5, 2019

Create a connection to WordPress MySQL database in PHP Score: 5 / 5 (1vote)
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp
The Author
Published by
Martin Jost

View all posts by Martin Jost

Related Posts
A
WordPress Add custom classes to the WordPress BODY tag

Anyone who develops their own theme with a customizable layout or settings a theme needs to add ...

Read More
G
PHP Get Metadata from WordPress Video and Audio files

In addition to images, videos and audio files are other formats that we can upload to WordPress ...

Read More
G
PHP Get the current WordPress URL

In some cases, it may be necessary to get the current WordPress URL of where the user is current ...

Read More
Read more
G
Previous Post
Get the installation URL of WordPress
G
Next Post
Get the absolute File Path (Document Root-Path) in WordPress
Leave a Reply Cancel reply

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

  • Imprint
  • Privacy Policy
© 2016-2021 - wphave - The Must Have for WordPress
Login

Lost your password?

RequiredEssential cookies enable basic functions and are necessary for the proper functioning of the website.
  • Name: Cookie Status
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user's web browser allows cookies.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookie_status
  • Duration: 1 Minute
  • Name: Cookie Consent
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user agrees to allow opt-in cookies or not.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookieconsent_analytics, cookieconsent_marketing, cookieconsent_status
  • Duration: 1 Year
  • Name: Like Button
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already gave a like for a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): like_btn_{POST_ID}
  • Duration: 21 Days
  • Name: Post Rating
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already rate a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): rating_{POST_ID}
  • Duration: 7 Days
AnalyticsStatistics cookies collect information anonymously. This information helps us to understand how our visitors use our website.
  • Name: Google Analytics
  • Provider: Google LLC
  • Purpose: Google cookie for website analysis. Generates statistical data on how the visitor uses the website.
  • Privacy Policy: https://policies.google.com/privacy
  • Cookie Name(s): _ga, _gat, _gid
  • Duration: 2 Years