maybe_create_table

Create database table, if it doesn't already exist.

Description

maybe_create_table( (string) $table_name, (string) $create_ddl ); 

Parameters (2)

0. $table_name (string)
Database table name.
1. $create_ddl (string)
Create database table SQL.

Usage

  1. if ( !function_exists( 'maybe_create_table' ) ) { 
  2. require_once ABSPATH . '/wp-admin/install-helper.php'; 
  3.  
  4. // Database table name. 
  5. $table_name = ''; 
  6.  
  7. // Create database table SQL. 
  8. $create_ddl = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = maybe_create_table($table_name, $create_ddl); 
  12.  

Defined (2)

The function is defined in the following location(s).

/wp-admin/install-helper.php  
  1. function maybe_create_table($table_name, $create_ddl) { 
  2. global $wpdb; 
  3. foreach ($wpdb->get_col("SHOW TABLES", 0) as $table ) { 
  4. if ($table == $table_name) { 
  5. return true; 
  6. // Didn't find it, so try to create it. 
  7. $wpdb->query($create_ddl); 
  8.  
  9. // We cannot directly tell that whether this succeeded! 
  10. foreach ($wpdb->get_col("SHOW TABLES", 0) as $table ) { 
  11. if ($table == $table_name) { 
  12. return true; 
  13. return false; 
/wp-admin/includes/upgrade.php  
  1. function maybe_create_table($table_name, $create_ddl) { 
  2. global $wpdb; 
  3.  
  4. $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) ); 
  5.  
  6. if ( $wpdb->get_var( $query ) == $table_name ) { 
  7. return true; 
  8.  
  9. // Didn't find it try to create it.. 
  10. $wpdb->query($create_ddl); 
  11.  
  12. // We cannot directly tell that whether this succeeded! 
  13. if ( $wpdb->get_var( $query ) == $table_name ) { 
  14. return true; 
  15. return false;