After trial and error, here is the code that works:
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' );
The installer.php looks somewhat like this:
<?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 );
}
?>