如果数据库中尚不存在,则在数据库中创建一个表。
原型
maybe_create_table( string $table_name, string $create_ddl )
描述
此方法检查现有数据库,如果尚未存在,则创建一个新数据库。它不依赖于MySQL的“IF NOT EXISTS”语句,而是选择先查询所有表,然后运行创建表的SQL语句。
参数
$table_name
(string)
(Required)
要创建的数据库表名称。
$create_ddl
(string)
(Required)
用于创建表的SQL语句。
返回值
(bool)
如果表已存在或由函数创建。
源文件
路径:wp-admin/includes/upgrade.php
<?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 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;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/maybe_create_table/