Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



maybe_create_table ›

Seit1.0.0
Veraltetn/v
maybe_create_table ( $table_name, $create_ddl )
Parameter: (2)
  • (string) $table_name Database table name.
    Erforderlich: Ja
  • (string) $create_ddl SQL statement to create table.
    Erforderlich: Ja
Gibt zurück:
  • (bool) True on success or if the table already exists. False on failure.
Definiert in:
Codex:

Creates a table in the database if it doesn't already exist.



Quellcode

function maybe_create_table( $table_name, $create_ddl ) {
		global $wpdb;

		foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
			if ( $table === $table_name ) {
				return true;
			}
		}

		// Didn't find it, so try to create it.
		$wpdb->query( $create_ddl );

		// We cannot directly tell that whether this succeeded!
		foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
			if ( $table === $table_name ) {
				return true;
			}
		}

		return false;
	}
endif;

if ( ! function_exists( 'maybe_add_column' ) ) :
	/**
	 * Adds column to database table, if it doesn't already exist.
	 *
	 * @since 1.0.0
	 *
	 * @global wpdb $wpdb WordPress database abstraction object.
	 *
	 * @param string $table_name  Database table name.
	 * @param string $column_name Table column name.
	 * @param string $create_ddl  SQL statement to add column.
	 * @return bool True on success or if the column already exists. False on failure.
	 */