在WordPress插件中我有此代码
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $wpdb;
global $country_table;
global $cities_table;
$AO_name = "trackbyid_dbv";
$AO_value = "1";
add_option($AO_name, $AO_value);
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $country_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
country tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ( ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$sql = "CREATE TABLE $cities_table(
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
city tinytext NOT NULL,
country_id mediumint(9) NOT NULL,
city_latitude mediumint(9) NOT NULL,
city_longitude mediumint(9) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
dbDelta($sql);
}
我认为我的问题(没有创建数据库)与全局$ country_table和$ cities_table变量有关。当我将这些变量作为本地问题时,问题就解决了!我的全局变量声明和用法有什么问题?
您的变量$country_table
和$cities_table
无法定义,因为在尝试在这些var中使用它之前,您没有在全局$ wpdb中引入。改用此方法:
global $wpdb;
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $country_table, $cities_table;
...
}