DBDelta未在插件数据库更新中添加其他列

时间:2017-02-13 作者:Himanshu

我试图通过添加额外的列字段来更新数据库。然而,dBdelta似乎没有做好它的工作。我不知道我做错了什么。

<?php

function installer(){
    include(\'installer.php\');
}
register_activation_hook( __file__, \'installer\' ); //executes installer php when installing plugin to create new database

//database update checkdate
function myplugin_update_db_check() {
    global $xenonresult_db_version;
    if ( get_site_option( \'xenonresult_db_version\' ) != $xenonresult_db_version ) {
        installer();
    }
}
add_action( \'plugins_loaded\', \'myplugin_update_db_check\' );
这是安装程序。php

<?php
    global $wpdb;
    $table_name = $wpdb->prefix . "xenonresult";
    $xenonresult_db_version = \'1.1\';
    $charset_collate = $wpdb->get_charset_collate();

    if ( $wpdb->get_var( "SHOW TABLES LIKE \'{$table_name}\'" ) != $table_name ) {
        $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );
        add_option( "xenonresult_db_version", $xenonresult_db_version );
    }

global $wpdb;
$installed_ver = get_option( "xenonresult_db_version" );
if ( $installed_ver != $xenonresult_db_version ) {

    $table_name = $wpdb->prefix . \'xenonresult\';

    $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        `mobile` varchar NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );

    update_option( "xenonresult_db_version", $xenonresult_db_version );
}
?>
我的默认版本是1.0。在1.1中,我试图在数据库中添加mobile列。

1 个回复
SO网友:Himanshu

经过反复试验,以下是有效的代码:

function installer(){
    include(\'installer.php\');
}
register_activation_hook( __file__, \'installer\' ); //executes installer php when installing plugin to create new database

//database update checkdate
function myplugin_update_db_check() {
    global $xenon_result_db_version;
    if ( get_option( \'xenon_result_db_version\' ) != $xenon_result_db_version ) {
        installer();
    }
}

add_action( \'plugins_loaded\', \'myplugin_update_db_check\' );
安装程序。php看起来有点像:

<?php
    global $wpdb;
    $table_name = $wpdb->prefix . "xenon_result";
    $xenon_result_db_version = \'1.2\';
    $charset_collate = $wpdb->get_charset_collate();
    if ( $wpdb->get_var( "SHOW TABLES LIKE \'{$table_name}\'" ) != $table_name ) {
        $sql = "CREATE TABLE $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `mobile` varchar(55) NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `total_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );
        add_option( "xenon_result_db_version", $xenon_result_db_version );
}
if ( get_option( \'xenon_result_db_version\' ) != $xenon_result_db_version ){
$sql = "CREATE TABLE $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `mobile` varchar(55) NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `total_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );
        update_option( "xenon_result_db_version", $xenon_result_db_version );
}
?>

相关推荐

将MySQL转换为WordPress$WPDB

我正在从3.7升级一个古老的Wordpress网站。它有一些SQL查询请求,在3.9以上版本之后无法运行,您现在必须通过$WPDB连接到DB(https://codex.wordpress.org/Class_Reference/wpdb). 我对此进行了初步尝试,但挂断了mysql\\u fetch\\u array部分的连接,也不清楚我所做的是否正确。mysql\\u fetch\\u数组似乎需要使用get\\u结果(https://developer.wordpress.org/reference/