· 8 years ago · Mar 08, 2018, 11:18 AM
1<?php
2
3function installer(){
4 include('installer.php');
5}
6register_activation_hook( __file__, 'installer' ); //executes installer php when installing plugin to create new database
7
8//database update checkdate
9function myplugin_update_db_check() {
10 global $xenonresult_db_version;
11 if ( get_site_option( 'xenonresult_db_version' ) != $xenonresult_db_version ) {
12 installer();
13 }
14}
15add_action( 'plugins_loaded', 'myplugin_update_db_check' );
16
17<?php
18 global $wpdb;
19 $table_name = $wpdb->prefix . "xenonresult";
20 $xenonresult_db_version = '1.1';
21 $charset_collate = $wpdb->get_charset_collate();
22
23 if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) != $table_name ) {
24 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
25 `id` int(11) NOT NULL AUTO_INCREMENT,
26 `roll_number` int(11) NOT NULL,
27 `student_name` text NOT NULL,
28 `father_name` text NOT NULL,
29 `obj_marks` int(9) NOT NULL,
30 `sub_marks` int(9) NOT NULL,
31 `result` text NOT NULL,
32 PRIMARY KEY (`id`)
33 ) $charset_collate;";
34 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
35 dbDelta( $sql );
36 add_option( "xenonresult_db_version", $xenonresult_db_version );
37 }
38
39global $wpdb;
40$installed_ver = get_option( "xenonresult_db_version" );
41if ( $installed_ver != $xenonresult_db_version ) {
42
43 $table_name = $wpdb->prefix . 'xenonresult';
44
45 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
46 `id` int(11) NOT NULL AUTO_INCREMENT,
47 `roll_number` int(11) NOT NULL,
48 `student_name` text NOT NULL,
49 `father_name` text NOT NULL,
50 `obj_marks` int(9) NOT NULL,
51 `sub_marks` int(9) NOT NULL,
52 `result` text NOT NULL,
53 `mobile` varchar NOT NULL,
54 PRIMARY KEY (`id`)
55 ) $charset_collate;";
56 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
57 dbDelta( $sql );
58
59 update_option( "xenonresult_db_version", $xenonresult_db_version );
60}
61?>
62
63function installer(){
64 include('installer.php');
65}
66register_activation_hook( __file__, 'installer' ); //executes installer php when installing plugin to create new database
67
68//database update checkdate
69function myplugin_update_db_check() {
70 global $xenon_result_db_version;
71 if ( get_option( 'xenon_result_db_version' ) != $xenon_result_db_version ) {
72 installer();
73 }
74}
75
76add_action( 'plugins_loaded', 'myplugin_update_db_check' );
77
78<?php
79 global $wpdb;
80 $table_name = $wpdb->prefix . "xenon_result";
81 $xenon_result_db_version = '1.2';
82 $charset_collate = $wpdb->get_charset_collate();
83 if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) != $table_name ) {
84 $sql = "CREATE TABLE $table_name (
85 `id` int(11) NOT NULL AUTO_INCREMENT,
86 `roll_number` int(11) NOT NULL,
87 `student_name` text NOT NULL,
88 `father_name` text NOT NULL,
89 `mobile` varchar(55) NOT NULL,
90 `obj_marks` int(9) NOT NULL,
91 `sub_marks` int(9) NOT NULL,
92 `total_marks` int(9) NOT NULL,
93 `result` text NOT NULL,
94 PRIMARY KEY (`id`)
95 ) $charset_collate;";
96 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
97 dbDelta( $sql );
98 add_option( "xenon_result_db_version", $xenon_result_db_version );
99}
100if ( get_option( 'xenon_result_db_version' ) != $xenon_result_db_version ){
101$sql = "CREATE TABLE $table_name (
102 `id` int(11) NOT NULL AUTO_INCREMENT,
103 `roll_number` int(11) NOT NULL,
104 `student_name` text NOT NULL,
105 `father_name` text NOT NULL,
106 `mobile` varchar(55) NOT NULL,
107 `obj_marks` int(9) NOT NULL,
108 `sub_marks` int(9) NOT NULL,
109 `total_marks` int(9) NOT NULL,
110 `result` text NOT NULL,
111 PRIMARY KEY (`id`)
112 ) $charset_collate;";
113 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
114 dbDelta( $sql );
115 update_option( "xenon_result_db_version", $xenon_result_db_version );
116}
117?>
118
119// Get the default value from the array.
120 if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
121 $default_value = $matches[1];
122 if ($tablefield->Default != $default_value) {
123 // Add a query to change the column's default value
124 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
125 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
126 }
127 }