WordPress开发者和主题制作者的搜索引擎



maybe_add_column ›

Since1.0.0
已弃用n/a
maybe_add_column ( $table_name, $column_name, $create_ddl )
参数: (3)
  • (string) $table_name Database table name
    Required: Yes
  • (string) $column_name Table column name
    Required: Yes
  • (string) $create_ddl SQL to add column to table.
    Required: Yes
返回:
  • (bool) False on failure. True, if already exists or was successful.
定义在:
文档:

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



源码

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;
	}
endif;