Get prefix from multiple wordpress sites with different databases with external script

I am trying to get the prefix of multiple databases of wordpress installations with different prefixes. But failing to do this and is getting me only the first table prefix for all files:

Folder structure (basic – has all required wordpress files):

Read More
home:
+---cpanel1
|   ---public_html
|       +---wp-load.php => $table_prefix = 'wp1_';
+---cpanel2
|   ---public_html
|       +---wp-load.php => $table_prefix = 'wp2_';
---cpanel3
    ---public_html
        +---wp-load.php => $table_prefix = 'wp3_';
+---get_prefixes.php

Script get_prefixes.php (PHP):

$myfile = '/wp-load.php';
class execute_sql_code {
    public function __construct($myfile) {
        $this->main_work($myfile);
    }
    public $cpanels = array(
            'cpanel1',
            'cpanel2',
            'cpanel3'
    );
    public function main_work($myfile) {
        foreach ($this->cpanels as $cpanel) {
            $the_file = $cpanel.'/public_html'.$myfile;
            var_dump($the_file);
            require($the_file);
            global $wpdb;
            var_dump($wpdb->prefix);
        }
    }
}
new execute_sql_code($myfile);

var_dump($the_file) => OUTPUTS CORRECTLY every path.

var_dump($wpdb->prefix) => OUTPUTS WRONG wp1_ for each iteration:

CURRENT OUTPUT:

Iteration 1 => cpanel1/public_html/wp-load.php => $wpdb->prefix = wp1_

Iteration 2 => cpanel2/public_html/wp-load.php => $wpdb->prefix = wp1_

Iteration 3 => cpanel3/public_html/wp-load.php => $wpdb->prefix = wp1_

EXPECTED OUTPUT:

Iteration 1 => cpanel1/public_html/wp-load.php => $wpdb->prefix = wp1_

Iteration 2 => cpanel2/public_html/wp-load.php => $wpdb->prefix = wp2_ // normal

Iteration 3 => cpanel3/public_html/wp-load.php => $wpdb->prefix = wp3_ // normal

Related posts

2 comments

  1. Try this, you need to change the array("username", "password", "DB1", "localhost") etc, config to connect with the multiple DB’s . also make sure your every folder’s cpanel wp-load.php file load successfuly.

    Using new wpdb() class will create a new object to play with current database or db functions.

    For more info i would like to suggest wordpress stackexchange

    $myfile = 'wp-load.php';
    
    class execute_sql_code
    {
        public function __construct($myfile)
        {
            $this->main_work($myfile);
        }
    
        public $cpanels = array(
           'cpanel1' => array("username", "password", "DB1", "localhost"),
           'cpanel2' => array("username", "password", "DB2", "localhost"),
           'cpanel3' => array("username", "password", "DB3", "localhost"),
        );
    
    
        public function main_work($myfile)
        {
            $i = 0;
            $cpanelKeys = array_keys($this->cpanels);
            foreach ($this->cpanels as $cpanel) {
    
                $path = $cpanelKeys[$i];
                $dbuser = ($cpanel[0]);
                $dbpassword = ($cpanel[1]);
                $dbname = ($cpanel[2]);
                $dbhost = ($cpanel[3]);
    
                $the_file = $_SERVER['DOCUMENT_ROOT'] . '/' . $path . '/public_html/' . $myfile;
                include_once($the_file);
    
                $wpdb = new wpdb($dbuser, $dbpassword, $dbname, $dbhost);
                $table_name = empty($wpdb->prefix) ? 'wp_' . "options" : $wpdb->prefix . "options";
                $sql = "SELECT option_value FROM " . $table_name . " WHERE option_name = 'siteurl';";
                $root_url = $wpdb->get_var($sql);
                var_dump($root_url);
                echo "<br>";
                unset($wpdb);
                $i++;
            }
        }
    }
    
    $obj = new execute_sql_code($myfile);
    
  2. I would suspect that PHP cannot overwrite object $wpdb once its created in global namespace. Try to unset($wpdb) after var_dump($wpdb->prefix):

    public function main_work($myfile) {
        foreach ($this->cpanels as $cpanel) {
            $the_file = $cpanel.'/public_html'.$myfile;
            var_dump($the_file);
            require($the_file);
            global $wpdb;
            var_dump($wpdb->prefix);
            unset($wpdb);
            global $table_prefix;
            unset($table_prefix);
        }
    }
    

    EDIT:

    Probably some additional globals are created and you have to check $GLOBALS before and after require($the_file); to deduce which to unset. I would suggest trying unset($table_prefix); for example.

Comments are closed.