maybe_create_table

函数
maybe_create_table ( $table_name, $create_ddl )
参数
  • (string) $table_name Database table name.
    Required:
  • (string) $create_ddl SQL statement to create table.
    Required:
返回值
  • (bool) True on success or if the table already exists. False on failure.
定义位置
相关方法
_oembed_create_xmlwp_create_tagwpmu_create_blogmaybe_convert_table_to_utf8mb4_maybe_update_themes
引入
1.0.0
弃用
-

maybe_create_table: 这是WordPress中的一个函数,它允许你创建一个数据库表,如果它还不存在: 这个函数检查该表是否存在,如果不存在,它就创建该表。

在数据库中创建一个表,如果它还不存在的话。

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.
	 */

常见问题

FAQs
查看更多 >