函数原型
maybe_create_table( string $table_name, string $create_ddl ): bool
函数描述
Creates a table in the database, if it doesn’t already exist.
是否弃用
未弃用
函数参数
-
$table_name
string
Required - Database table name.
-
$create_ddl
string
Required - SQL statement to create table.
函数返回值
bool True on success or if the table already exists. False on failure.
函数位置
File: wp-admin/includes/upgrade.php.
函数源码
function maybe_create_table( $table_name, $create_ddl ) {
global $wpdb;
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
if ( $wpdb->get_var( $query ) === $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!
if ( $wpdb->get_var( $query ) === $table_name ) {
return true;
}
return false;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |

