check_column

函式
check_column ( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null )
引數
  • (string) $table_name Database table name.
    Required:
  • (string) $col_name Table column name.
    Required:
  • (string) $col_type Table column type.
    Required:
  • (bool) $is_null Optional. Check is null.
    Required:
    Default: null
  • (mixed) $key Optional. Key info.
    Required:
    Default: null
  • (mixed) $default_value Optional. Default value.
    Required:
    Default: null
  • (mixed) $extra Optional. Extra value.
    Required:
    Default: null
返回值
  • (bool) True, if matches. False, if not matching.
定義位置
相關方法
check_commentthe_commentcheck_comment_flood_dbwp_check_comment_floodmaybe_add_column
引入
1.0.0
棄用
-

check_column: 這個函式檢查一個列是否支援特定的螢幕或文章型別。它被WordPress的管理螢幕使用,以確定在一個特定的螢幕上應該顯示哪些列。

檢查資料庫表列是否符合標準。

使用SQL DESC來檢索該列的表資訊。如果你對SQL語句返回的列資訊做更多的研究,這將有助於理解引數。傳入null以跳過檢查該標準。

從DESC表返回的列名是區分大小寫的,並被列出。
Field
Type
Null
Key
Default
Extra

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) {
global $wpdb;
$diffs = 0;
$results = $wpdb->get_results( "DESC $table_name" );
foreach ( $results as $row ) {
if ( $row->Field === $col_name ) {
// Got our column, check the params.
if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
++$diffs;
}
if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
++$diffs;
}
if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
++$diffs;
}
if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) {
++$diffs;
}
if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
++$diffs;
}
if ( $diffs > 0 ) {
return false;
}
return true;
} // End if found our column.
}
return false;
}
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) { global $wpdb; $diffs = 0; $results = $wpdb->get_results( "DESC $table_name" ); foreach ( $results as $row ) { if ( $row->Field === $col_name ) { // Got our column, check the params. if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) { ++$diffs; } if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) { ++$diffs; } if ( ( null !== $key ) && ( $row->Key !== $key ) ) { ++$diffs; } if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) { ++$diffs; } if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) { ++$diffs; } if ( $diffs > 0 ) { return false; } return true; } // End if found our column. } return false; }
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) {
	global $wpdb;

	$diffs   = 0;
	$results = $wpdb->get_results( "DESC $table_name" );

	foreach ( $results as $row ) {

		if ( $row->Field === $col_name ) {

			// Got our column, check the params.
			if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
				++$diffs;
			}
			if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
				++$diffs;
			}
			if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
				++$diffs;
			}
			if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) {
				++$diffs;
			}
			if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
				++$diffs;
			}

			if ( $diffs > 0 ) {
				return false;
			}

			return true;
		} // End if found our column.
	}

	return false;
}

常見問題

FAQs
檢視更多 >