maybe_add_column()

最后更新于:2021-11-27 14:41:13

maybe_add_column( string$table_name, string$column_name, string$create_ddl)

Adds column to a database table, if it doesn’t already exist.

参数

$table_name

(string) (Required) Database table name.

$column_name

(string) (Required) Table column name.

$create_ddl

(string) (Required) SQL statement to add column.

响应

(bool) True on success or if the column already exists. False on failure.

源文件

文件: gc-admin/includes/upgrade.php

function maybe_add_column( $table_name, $column_name, $create_ddl ) {
	global $gcdb;

	foreach ( $gcdb->get_col( "DESC $table_name", 0 ) as $column ) {
		if ( $column === $column_name ) {
			return true;
		}
	}

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

	// We cannot directly tell that whether this succeeded!
	foreach ( $gcdb->get_col( "DESC $table_name", 0 ) as $column ) {
		if ( $column === $column_name ) {
			return true;
		}
	}

	return false;
}