Halloween Sale 15% off Ends in See the plans
PHP

Convert MyISAM Database Tables to InnoDB

Run on demand to convert your site's remaining MyISAM tables to InnoDB, the modern MySQL engine that handles many visitors at once far better and survives crashes more safely.

Don’t have WPIDE yet? Install it on your site

Code

global $wpdb;

$wpide_tables = $wpdb->get_col( $wpdb->prepare(
    "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND ENGINE = 'MyISAM' AND TABLE_NAME LIKE %s",
    DB_NAME,
    $wpdb->esc_like( $wpdb->prefix ) . '%'
) );

if ( ! $wpide_tables ) {
    echo 'All tables already use InnoDB.';
} else {
    foreach ( $wpide_tables as $wpide_table ) {
        $wpide_ok = false !== $wpdb->query( 'ALTER TABLE `' . esc_sql( $wpide_table ) . '` ENGINE=InnoDB' );
        printf( "%s: %s\n", esc_html( $wpide_table ), $wpide_ok ? 'converted' : 'failed (' . esc_html( $wpdb->last_error ) . ')' );
    }
}

Notes

Back up the database first and run it during a quiet period, since large tables take a while; tables already using InnoDB are skipped.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.