Wpdb 2simple 2sample

function prefix_update_table() {
// Assuming we have our current database version in a global variable
global $prefix_my_db_version;

// If database version is not the same
if ( $prefix_my_db_version != get_option(‘prefix_my_plugin_db_version’ ) {
global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE my_custom_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
first_name varchar(55) NOT NULL,
last_name varchar(55) NOT NULL,
phone varchar(32) DEFAULT ” NOT NULL, //new column
email varchar(55) NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";

if ( ! function_exists(‘dbDelta’) ) {
require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
}

dbDelta( $sql );

update_option( ‘prefix_my_plugin_db_version’, $prefix_my_db_version );
}
}

Leave a comment