Simple wordpress function to change wordpress theme via database, easy php code to change wordpress theme in mysql, two ways.

How To Change WordPress Theme Via MySQL Database

Two ways to change wordpress theme via mysql database, first way using global $wpdb and query() function, second way using update_option() function only.

First Way

Using global $wpdb and query() function:

function WPTime_change_theme_in_database(){
    global $wpdb;
    $wpdb->query(" UPDATE $wpdb->options SET option_value = 'twentyfifteen' WHERE option_name in ('template', 'stylesheet', 'current_theme') ");
    /* Now Twenty Fifteen theme will be activated */
}

WPTime_change_theme_in_database();

Change ‘twentyfifteen’ to your theme name (same theme folder name) for example ‘twentythirteen’.

Second Way

Using update_option() function:

function WPTime_change_theme_via_database(){
    update_option('current_theme', 'twentyfifteen');
    update_option('template', 'twentyfifteen');
    update_option('stylesheet', 'twentyfifteen');
    /* Now Twenty Fifteen theme will be activated */
}

WPTime_change_theme_via_database();

Change ‘twentyfifteen’ to your theme name (same theme folder name).