Winner of the Best WordPress Business Website - WP Honors 2011
Since 2004
 

Forums

 

Manuals

 

Videos

 

Live Support

 

WPMU.org

Accessing a non-WP MySQL database from inside WP?

Elite

I want to be able to query a non-WordPress MySQL database from within WP. Is this a safe practice, or will it break WP? Is there a good "best-practice" way to do this to make sure the databases don't get confused?

Everyday at WPMU DEV we help hundreds of WP Users. Become a member today to:

  • Download 250+ Premium Plugins and Themes
  • Get unlimited support from WordPress Experts
  • Get help with your WordPress or BuddyPress projects

Help & Support from WPMU DEV members and staff

Staff

You just have to use normal mysql db connection code.

Elite

I do this all the time, you can use the global $wpdb class to prepare and get_var etc

if your other database has the same user/password details as your wp one (like a global access details) then you can just reference the table in the non-wp database by using databasename.tablename in the FROM statement like this

$query = $wpdb->prepare('SELECT somevar FROM somedb.sometable WHERE someothervar = %d AND evenonemorevar = %s AND somevartitle = %s LIMIT 1',$intnum,'string',$stringvar);

$wpdb->get_col($query);
or
$wpdb->get_results($query);
etc.

if the non-wp db and the wp-db are using different access details, you should be able to add the wp db user to the non-wp db and $wpdb should be quite happy to access data from both.

Get personal, comprehensive and timely support and assistance
from WordPress Experts

Staff

Very clever! Didn't think about that.

Keeper of the Dark Chocolate

We just hard code in the mysql commands.

We can help you out. Become a member today to:

  • Let our team help you out today and access to over 250+ Premium Plugins and Themes
  • Find out why people saying "it's like having your own developer"
  • WPMU DEV - The WordPress Experts