maybe_add_column

函式
maybe_add_column ( $table_name, $column_name, $create_ddl )
引數
  • (string) $table_name Database table name.
    Required:
  • (string) $column_name Table column name.
    Required:
  • (string) $create_ddl SQL statement to add column.
    Required:
返回值
  • (bool) True on success or if the column already exists. False on failure.
定義位置
相關方法
maybe_drop_columnimage_add_captionget_hidden_columnsmaybe_hash_hex_color_maybe_update_core
引入
1.0.0
棄用
-

maybe_add_column: 這是WordPress中的一個函式,允許你在資料庫表中新增一個列,如果它還不存在的話: 這個函式檢查一個列是否存在於指定的表中,如果不存在,它就把這個列新增到表中。

為資料庫表新增列,如果它還不存在的話。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
/**
* Drops column from database table, if it exists.
*
* @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 $drop_ddl SQL statement to drop column.
* @return bool True on success or if the column doesn't exist. False on failure.
*/
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; /** * Drops column from database table, if it exists. * * @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 $drop_ddl SQL statement to drop column. * @return bool True on success or if the column doesn't exist. False on failure. */
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;

/**
 * Drops column from database table, if it exists.
 *
 * @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 $drop_ddl    SQL statement to drop column.
 * @return bool True on success or if the column doesn't exist. False on failure.
 */

常見問題

FAQs
檢視更多 >