I'm trying to get a connection to a separate external database from the database the wordpress site depends on. I have a PHP file inside the same location of my theme, it is basically a blank PHP it has the needed PHP tags.

I'm trying to make a connection using what I found online this is what the whole file looks like:

$mydb = new wpdb('username','password','db_name','xxx.xxx.xxx');

When I try to make a connection with the correct credentials I get an error in my WPEngine Live Site Log that says:

PHP Fatal error: Class 'wpdb' not found in /nas/content/live/...

How do I go about fixing this issue, so that I can connect to my external database?

  • That looks like it should work. How are you calling this code? Is it in a theme, a plugin, ...? If possible, please edit your question to provide some more context. – Pat J May 27 '16 at 19:11
  • @PatJ I extended/provided more details – RTarson May 27 '16 at 19:16
  • @PatJ Do I have to make an include tag so it runs wp-db.php? – RTarson May 27 '16 at 19:24
  • Instead of putting your php in a separate file add it to your wp-config file and you should be all set. You may need to make $mydb a global but, I think that's it; looking for an old set up I did for more details . . . – dg4220 May 27 '16 at 19:41
  • @dg4220 I think I found a solution! One sec, I'll get back to you! – RTarson May 27 '16 at 19:42

My issue was resolved, not sure if the connection to the database will work, but the error with wpdb connection was fixed. It was quite easy fix once I found out what the file dependence was. This is what you need to do:

In the file you are trying to use wpdb you need to add code before you can use the class:

<?php require_once('../../../wp-load.php'); ?>

Note* You may need to change how far back in the directory you go depending on your install or depending on the location of your php template/file.

It should run wpdb without any errors.

  • You saved me a total hour of work. Thanks man 🙏 – chainstair Aug 5 '20 at 13:12

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.