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):
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_
// normalIteration 3 => cpanel3/public_html/wp-load.php =>
$wpdb->prefix = wp3_
// normal
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 cpanelwp-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
I would suspect that PHP cannot overwrite object
$wpdb
once its created in global namespace. Try tounset($wpdb)
aftervar_dump($wpdb->prefix)
:EDIT:
Probably some additional globals are created and you have to check
$GLOBALS
before and afterrequire($the_file);
to deduce which to unset. I would suggest tryingunset($table_prefix);
for example.