Wordpress - Checking if Database Table exists

If you use "IF NOT EXISTS" then the dbdelta script will not upgrade your database with delta's appeared after the initial creation of the database.

(assuming you want to re-use the same sql script)

at least... that is what i think


DISCLAIMER : I'm not a WordPress Guru, only a MySQL DBA

If you want to user a different query, try this

SELECT COUNT(1) FROM information_schema.tables WHERE table_schema='dbname' AND table_name='tbname';

It will either return 0 (if table does not exist) or 1 (if table does exist)


Try this one:

global $wpdb;
$table_name = $wpdb->base_prefix.'custom_prices';
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );

if ( ! $wpdb->get_var( $query ) == $table_name ) {
    // go go
}

Tags:

Mysql

Database