我刚开始开发一个插件。但是当我更改MySQL查询时dbDelta()不起作用,之后我试图进一步停用并激活插件,但是表没有使用dbDelta()finction更新。
if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
$sql = 'CREATE TABLE '.$table_name.' (
id int(11) NOT NULL AUTO_INCREMENT,
product_name text NOT NULL,
UNIQUE KEY id (id))';
//reference to upgrade.php file
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
答案 0 :(得分:0)
通过直接调用它来检查表存在会更好......
global $wpdb;
$table_name= 'name of table without the prefix!'
if(!isset($wpdb->$table_name)){
$sql = 'CREATE TABLE '.$table_name.' (
id int(11) NOT NULL AUTO_INCREMENT,
product_name text NOT NULL,
UNIQUE KEY id (id))';
//reference to upgrade.php file
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$result=dbDelta($sql);
var_dump($result);
}