gpt4 book ai didi

php - 使用外部脚本从具有不同数据库的多个 wordpress 站点获取前缀

转载 作者:行者123 更新时间:2023-11-29 02:53:41 26 4
gpt4 key购买 nike

我正在尝试获取具有不同前缀的 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) => 输出正确每条路径。

var_dump($wpdb->prefix) => 每次迭代输出错误 wp1_:

当前输出:

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_

预期输出:

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

最佳答案

试试这个,你需要更改 array("username", "password", "DB1", "localhost") 等配置以连接多个数据库。还要确保每个文件夹的 cpanel wp-load.php 文件加载成功。

使用 new wpdb() 类将创建一个新对象来使用当前数据库或数据库函数。

有关更多信息,我想建议 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 - 使用外部脚本从具有不同数据库的多个 wordpress 站点获取前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32945300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com