wordpress检测表是否存在

如何检查一个表是否存在wordpress数据库中呢?


1
2
3
4
5
global $wpdb;
$table_name = "your-table-name-here";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
//table is not created. you may create the table here.
}

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$flag = true;
$message = array();
$table_name = $wpdb->prefix .$_POST['table'];
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$flag = false;
$table_name = $_POST['table'];
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
$flag = true;
}
}
if($flag){
foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name ) {
array_push($message, $column_name);
// error_log( $column_name );
}
}

 

坚持原创技术分享,您的支持将鼓励我继续创作!