[英]Get prefix from multiple wordpress sites with different databases with external script
我正在嘗試獲取具有不同前綴的 wordpress 安裝的多個數據庫的前綴。 但是沒有做到這一點,我只能得到所有文件的第一個表前綴:
文件夾結構(基本 - 包含所有必需的 wordpress 文件):
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
腳本 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正確的每條路徑。
var_dump($wpdb->prefix)
=> 每次迭代輸出錯誤wp1_
:
電流輸出:
迭代 1 => cpanel1/public_html/wp-load.php =>
$wpdb->prefix = wp1_
迭代 2 => cpanel2/public_html/wp-load.php =>
$wpdb->prefix = wp1_
迭代 3 => cpanel3/public_html/wp-load.php =>
$wpdb->prefix = wp1_
預期產出:
迭代 1 => cpanel1/public_html/wp-load.php =>
$wpdb->prefix = wp1_
迭代 2 => cpanel2/public_html/wp-load.php =>
$wpdb->prefix = wp2_
// 正常迭代 3 => cpanel3/public_html/wp-load.php =>
$wpdb->prefix = wp3_
// 正常
試試這個,你需要更改array("username", "password", "DB1", "localhost")
等,配置以連接多個數據庫。 還要確保您每個文件夾的 cpanel wp-load.php
文件加載成功。
使用new wpdb()
類將創建一個新對象來使用當前的數據庫或 db 函數。
有關更多信息,我想建議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);
我懷疑 PHP 在全局命名空間中創建后無法覆蓋對象$wpdb
。 嘗試在var_dump($wpdb->prefix)
之后unset($wpdb)
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);
}
}
編輯:
可能創建了一些額外的全局變量,您必須在require($the_file);
之前和之后檢查$GLOBALS
require($the_file);
推斷要取消設置的內容。 我建議嘗試unset($table_prefix);
例如。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.