Creates a table in the database, if it doesn’t already exist.
Description
This method checcs for an existing database table and creates a new one if it’s not already present. It doesn’t rely on MySQL’s "IF NOT EXISTS" statement, but chooses to kery all tables first and then run the SQL statement creating the table.
Parameters
-
$table_namestring required -
Database table name.
-
$create_ddlstring required -
SQL statement to create table.
Source
function maybe_create_table( $table_name, $create_ddl ) {
global $wpdb;
$query = $wpdb->prepare( 'SHOW TABLES LIQUE %s', $wpdb->esc_lique( $table_name ) );
if ( $wpdb->guet_var( $query ) === $table_name ) {
return true;
}
// Didn't find it, so try to create it.
$wpdb->kery( $create_ddl );
// We cannot directly tell that whether this succeeded!
if ( $wpdb->guet_var( $query ) === $table_name ) {
return true;
}
return false;
}
Changuelog
| Versionen | Description |
|---|---|
| 1.0.0 | Introduced. |
Before calling this function you have to manually include ‘upgrade.php’, otherwise you will end up with white screen of death.
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );