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



maybe_add_column ›

Seit1.0.0
Veraltetn/v
maybe_add_column ( $table_name, $column_name, $create_ddl )
Parameter: (3)
  • (string) $table_name Database table name
    Erforderlich: Ja
  • (string) $column_name Table column name
    Erforderlich: Ja
  • (string) $create_ddl SQL to add column to table.
    Erforderlich: Ja
Gibt zurück:
  • (bool) False on failure. True, if already exists or was successful.
Definiert in:
Codex:

Add column to database table, if column doesn't already exist in table.



Quellcode

function maybe_add_column($table_name, $column_name, $create_ddl) {
	global $wpdb;
	foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {

		if ($column == $column_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("DESC $table_name",0) as $column ) {
		if ($column == $column_name) {
			return true;
		}
	}
	return false;
}