使用外部脚本

时间:2015-10-05 09:33:35

标签: php mysql wordpress

我试图获得具有不同前缀的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_

CURRENT OUTPUT:

  

迭代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_ //正常

2 个答案:

答案 0 :(得分:1)

试试这个,您需要更改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);

答案 1 :(得分:0)

我怀疑PHP在全局命名空间中创建后无法覆盖对象$wpdb。在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);
    }
}

编辑:

可能会创建一些额外的全局变量,您必须在$GLOBALS之前和之后检查require($the_file);,以推断出哪个未设置。我建议尝试unset($table_prefix);