Accessing a non-WP MySQL database from inside WP?
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
You just have to use normal mysql db connection code.
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
Very clever! Didn't think about that.
We just hard code in the mysql commands.





