O'Brien Labs

  • Home
  • Portfolio
  • WordPress
    • Plugins
    • WordPress Tips
  • Random Post
  • About

Keyboard Shortcuts

R - Load a random post.
? - Open this dialog window.
O'Brien Labs > Tech Tips > WordPress – Accessing external database Keyboard Shortcuts

WordPress – Accessing external database

December 23, 2014 by Pat Leave a Comment

Share this post:

wordpress-logo-notext-rgb-250pxI wanted to post this quick tip on how to access an external database, or an external table, from within WordPress. This is mostly here for my reference in case I need it in the future. Hopefully it helps you out, too.

I have a table that’s external to my primary WordPress installation that I needed to get some information out of. In my functions.php, I added this piece of code into a function I already had, and it worked great.

// Setup external database call
$externalTable = new wpdb(DB_USER, DB_PASSWORD, 'tablename', 'localhost');
$externalTable->show_errors();
$exTable = $externalTable->get_results("SELECT * FROM data");
$dataCount = count($data);

It’s a simple bit of code that just counts the number of entries in the “data” table. This snippet uses the the database login and password information from your wp-config.php. If you need to specify different login information, just change the DB_USER and DB_PASSWORD to your values.

Share this post:

About Pat

I'm a sysadmin, WordPress Core Contributor, plugin & theme developer. I've also developed a few non-WordPress websites. I'm a technologist at heart. I enjoy tinkering on many projects. Here on my site you will find some of the projects I've worked on. Whether it's the WordPress plugins I've made, some WordPress tips, various tech tips, or even a couple Halloween props.

View all posts by Pat

Leave a Reply Cancel reply

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

About

The purpose of this site is for me to share my DIY technical experiences as a place to document them publicly. Hopefully somewhere along the way they are helpful for you.

Recent Posts

  • Nest Thermostat Push Notifications
  • mailx – Send automated Linux email with gmail
  • simplemonitor – who watches the Nagios watchman?
  • Raspberry Pi and Nagios for reliable low cost monitoring
  • Using Nagios to monitor your power outlets

Tags

ad adblock amp Asterisk AutoHotKey battery block Camera car code codesnips database developer Emergency halloween Heat Home Inverter Linux load Nagios night plugins portfolio Power Project Raspberry Pi simplemonitor snippets Tech Tips Timelapse tips Tutorial ublock vision Weather Station webcam website WordPress

Copyright © 2010–2018 O'Brien Labs – All Rights Reserved – Use without permission is illegal.

Please support this website by adding us to your whitelist in your ad blocker. Ads are what helps us bring you premium content! Thank you!