• Resolved Chris_topher

    (@chris_topher)


    I’m creating a plugin and part of the plugin requires that I connect to an external WordPress site and use that site’s external database to get some information. I have established a connection and I have accessed the external database’s $wpdb using the code below:

    global $new_wpdb;
    
    define( 'BLOCK_LOAD', true );
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
    $new_wpdb = new wpdb( $username, $password, $dbname, $servername);
    $new_wpdb->show_errors();

    When I use print_r($new_wpdb);, I get the array and I can see the values for the external database’s username, password, etc. However, there’s no value for prefix or base_prefix. It’s just displaying the following for the prefix:

    [prefix] => [base_prefix] =>
    How do I get the prefix that the external database is using?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    Unless the external site is on the same server as your plugin, there is no reliable-yet-simple way to obtain the information you want. The best you can do is run the following query:

    SHOW TABLES LIKE '%term_taxonomy%'

    and if it returns just one row, you can parse the result to get the table prefix. If it returns multiple rows, the database contains multiple WordPress installations (multisite), and things get much more difficult. At that point you’ll need to read data from each _options table (using the prefixes you parsed above) and hopefully find something which matches information you have locally.

    Thread Starter Chris_topher

    (@chris_topher)

    Thanks for clarifying that for me. The user of the plugin already needs to enter their credentials to connect to the external database. So I just added another field to the form asking for their prefix. I also included information in the ReadMe on how to find the prefix in case the user doesn’t know what it is. It wasn’t the method I was hoping to use, but it works.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘need to find an external database's prefix’ is closed to new replies.