I read the wordpress codex and professional wordpress. It seems both use something like
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
to determine if the table exists. Is there any reason why CREATE TABLE IF NOT EXISTS ( ... )
is not used? It will check and create the table in 1 query, won’t it be better? Or am I missing something?
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
Try this one:
DISCLAIMER : I’m not a WordPress Guru, only a MySQL DBA
If you want to user a different query, try this
It will either return 0 (if table does not exist) or 1 (if table does exist)
TL;DR
WordPress now provides the
maybe_create_table
function – see https://developer.wordpress.org/reference/functions/maybe_create_table/Before calling this function you have to manually include the
upgrade.php
file, otherwise you will get a fatal error like follows:Not sure how or why this works, but I can do:
I understand this is really old question, but maybe some one find it usefull.
Small calss that handle function on new DB, version of table and create if does not exists.
Use
get_var
function fromwpdb
class with an exception handling:Reference: SELECT a Variable