maybe_convert_table_to_utf8mb4() - WordPress 函数参考中文文档

返回介绍

maybe_convert_table_to_utf8mb4()

发布于 2017-09-11 字数2195 浏览 956 评论 0

maybe_convert_table_to_utf8mb4( string $table )

If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.


描述


参数

$table

(string)
(Required)
The table to convert.


返回值

(bool) true if the table was converted, false if it wasn’t.


源代码

File: wp-admin/includes/upgrade.php

function maybe_convert_table_to_utf8mb4( $table ) {
	global $wpdb;

	$results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" );
	if ( ! $results ) {
		return false;
	}

	foreach ( $results as $column ) {
		if ( $column->Collation ) {
			list( $charset ) = explode( '_', $column->Collation );
			$charset = strtolower( $charset );
			if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ) {
				// Don't upgrade tables that have non-utf8 columns.
				return false;
			}
		}
	}

	$table_details = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table'" );
	if ( ! $table_details ) {
		return false;
	}

	list( $table_charset ) = explode( '_', $table_details->Collation );
	$table_charset = strtolower( $table_charset );
	if ( 'utf8mb4' === $table_charset ) {
		return true;
	}

	return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
}

更新日志

Version 描述
4.2.0 Introduced.

相关函数

Uses

  • wp-includes/wp-db.php:
    wpdb::get_results()
  • wp-includes/wp-db.php:
    wpdb::get_row()
  • wp-includes/wp-db.php:
    wpdb::query()

Used By

  • wp-admin/includes/upgrade.php:
    pre_schema_upgrade()
  • wp-admin/includes/upgrade.php:
    upgrade_network()

User Contributed Notes

上一篇:maybe_add_column()

下一篇:maybe_create_table()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!