我试图通过添加额外的列字段来更新数据库。然而,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列。
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 );
}
?>