maybe_create_table() - WordPress 函数参考中文文档

返回介绍

maybe_create_table()

发布于 2017-09-11 字数2020 浏览 755 评论 0

maybe_create_table( string $table_name,  string $create_ddl )

Creates a table in the database if it doesn’t already exist.


描述

This method checks for an existing database and creates a new one if it’s not already present. It doesn’t rely on MySQL’s "IF NOT EXISTS" statement, but chooses to query all tables first and then run the SQL statement creating the table.


参数

$table_name

(string)
(Required)
Database table name to create.

$create_ddl

(string)
(Required)
SQL statement to create table.


返回值

(bool) If table already exists or was created by function.


源代码

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 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 描述
1.0.0 Introduced.

相关函数

Uses

  • wp-includes/wp-db.php:
    wpdb::esc_like()
  • wp-includes/wp-db.php:
    wpdb::get_col()
  • wp-includes/wp-db.php:
    wpdb::query()
  • wp-includes/wp-db.php:
    wpdb::get_var()
  • wp-includes/wp-db.php:
    wpdb::prepare()

User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!