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中的一個函式,它允許你建立一個資料庫表,如果它還不存在: 這個函式檢查該表是否存在,如果不存在,它就建立該表。

在資料庫中建立一個表,如果它還不存在的話。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.
*/
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. */
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
檢視更多 >