我正在使用WP-CLI进行测试,以更新我的域名prod
,dev
,staging
,test
和update
站点。
我的wp-config
文件中有有条件的语句,然后我收到以下错误:
PHP Notice: Undefined index: HTTP_HOST in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(1070) : eval()'d code on line 22
PHP Notice: Undefined index: HTTP_HOST in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(1070) : eval()'d code on line 24
PHP Notice: Undefined index: HTTP_HOST in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(1070) : eval()'d code on line 26
PHP Notice: Undefined index: HTTP_HOST in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(1070) : eval()'d code on line 28
PHP Notice: Undefined variable: db_name in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(1070) : eval()'d code on line 34
Error: Can’t select database. We were able to connect to the database server (which means your username and password is okay) but not able to select the `` database.
这是我对wp-config.php
文件进行编码的方式!!!
if ($_SERVER["HTTP_HOST"] === 'example.com') {
$db_name = 'example';
}else if ($_SERVER["HTTP_HOST"] === 'dev.example.com') {
$db_name = 'example_dev';
}else if ($_SERVER["HTTP_HOST"] === 'staging.example.com'){
$db_name = 'example_staging';
}else if ($_SERVER["HTTP_HOST"] === 'update.example.com') {
$db_name = 'example_update';
}
define('DB_NAME', $db_name);
有什么办法可以让WP-CLI使用此文件?
$_SERVER["HTTP_HOST"]
从命令行使用PHP时,不存在。最好的选择是在命令行中使用文件时指定一个情况,例如:
if (php_sapi_name() == "cli") {
//set db_name when in cli
} else {
//set db_name as per host
if ($_SERVER["HTTP_HOST"] === 'example.com') {
$db_name = 'example';
}else if ($_SERVER["HTTP_HOST"] === 'dev.example.com') {
$db_name = 'example_dev';
}else if ($_SERVER["HTTP_HOST"] === 'staging.example.com'){
$db_name = 'example_staging';
}else if ($_SERVER["HTTP_HOST"] === 'update.example.com') {
$db_name = 'example_update';
}
}
我不太确定您想要$ db_name在WP-CLI时,我已经将其设置在第一组括号中。