0

I'm trying to access a second database with my wordpress system. To do this, I've attempted to follow the advice found here which seems to have worked for a lot of people.

In summary, I'm adding the following line to my functions.php:

global $newdb;
$newdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$newdb->show_errors();

Unfortunately, when I add this I come up with the following error:

Fatal error: Class 'wpdb' not found in /home/mine/www/wp-includes/functions.php

I'm sure I must be missing wrong (obviously, right?) but the instructions don't seem to have any extra steps that I've been leaving out.

Thanks for any help

11
  • did you make it global ? ..... global newdb ?
    – pl4g4
    Oct 4, 2013 at 19:03
  • @pl4g4 Thanks for the response! I did, and i'll add that to my description above.... I assume you're making sure that I had "global $newdb" preceding the rest of my $newdb code, right? Oct 4, 2013 at 19:12
  • yes, Remember that you have to declare $newdb to be a global variable in the function that you’re going to use it.
    – pl4g4
    Oct 4, 2013 at 19:17
  • Are your constants (DB_USER, DB_PASSWORD, etc) all defined somewhere? Are you calling new wpdb() in a filter or action, or just bare in the functions.php file?
    – Pat J
    Oct 4, 2013 at 19:18
  • @PatJ DB_USER and alike have their attributes hard-coded in (I simply used generic terms for interweb privacy purposes). The calling of new wpdb() is just bare in functions.php. Might that be an issue? Thanks for the reply! Oct 4, 2013 at 19:25

2 Answers 2

3

When enhancing WordPress functionality, always use the theme's functions.php file (located, generally, in {WP root}/wp-content/themes/{your theme}/functions.php), or put your code into a plugin.

The rules for hacking core code are similar to the rules for optimization.

  1. Don't do it.
  2. (for experts only) Don't do it yet.
2

try something like this

add_action('init', 'doSomething');
function doSomething(){
     global $newdb;
     $newdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
     $newdb->show_errors();
}
3
  • Alright, this is getting weird. Now it's giving me Fatal error: Call to undefined function add_action() That shouldn't be a problem, should it? Oct 4, 2013 at 19:32
  • that code is working for me. where r u adding the code? ... check what @Pat J said.
    – pl4g4
    Oct 4, 2013 at 19:40
  • yea..... you were on the right track with that question. PatJ above rightly chided me for editing my wp-includes file and pointed me in the right direction. Thanks for trying to help me bumble my way through this though! Oct 4, 2013 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.