A
A
Alexander Sobolev2018-09-11 11:33:37
MySQL
Alexander Sobolev, 2018-09-11 11:33:37

How to check if a table exists in mysql database?

Komrades, advise the most efficient and correct way to check the existence of a table

$wpdb->prefix . termmeta

in Wordpress database. Is it worth writing a rake with an unfulfilled request, or is there some standard "ping" or Wordpress function that I don't know about ..
There is a function that creates a table in the database, which needs to be run once upon activation, but the user, in the end, can activate the plugin several times, but writing a function to delete a table when the plugin is deactivated is not right - all of a sudden, deactivation is not needed for subsequent deletion ..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2018-09-11
@san_jorich

$wpdb->get_var("tableName")
if ( $wpdb->get_var("show tables like '".$table."'") != $table ) {
Create table;
}

V
Voenniy, 2018-09-12
@Voenniy

Is the table created with pure sql or wp wrapper?
If sql then you can immediately send
create table if not exist to the sql database without any checks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question