我读了wordpress codex和专业wordpress。似乎两者都使用
if($wpdb->get_var("SHOW TABLES LIKE \'$table_name\'") != $table_name) {
确定表是否存在。有什么原因吗CREATE TABLE IF NOT EXISTS ( ... )
是否未使用?它将在1个查询中检查并创建表,这不是更好吗?还是我遗漏了什么?我读了wordpress codex和专业wordpress。似乎两者都使用
if($wpdb->get_var("SHOW TABLES LIKE \'$table_name\'") != $table_name) {
确定表是否存在。有什么原因吗CREATE TABLE IF NOT EXISTS ( ... )
是否未使用?它将在1个查询中检查并创建表,这不是更好吗?还是我遗漏了什么?如果您使用“If NOT EXISTS”,那么dbdelta脚本将不会升级数据库,因为在初始创建数据库之后会出现delta。
(假设要重复使用相同的sql脚本)
至少我就是这么想的
Try this one:
global $wpdb;
$table_name = $wpdb->base_prefix.\'custom_prices\';
$query = $wpdb->prepare( \'SHOW TABLES LIKE %s\', $wpdb->esc_like( $table_name ) );
if ( ! $wpdb->get_var( $query ) == $table_name ) {
// go go
}
免责声明:我不是WordPress大师,只是MySQL DBA
如果要使用其他查询,请尝试以下操作
SELECT COUNT(1) FROM information_schema.tables WHERE table_schema=\'dbname\' AND table_name=\'tbname\';
它将返回0(如果表不存在)或1(如果表确实存在)TL;DR
require_once ABSPATH . \'wp-admin/includes/upgrade.php\';
global $wpdb;
$tablename = \'myTable\';
$main_sql_create = "CREATE TABLE $tablename";
maybe_create_table( $wpdb->prefix . $tablename, $main_sql_create );
WordPress现在提供maybe_create_table
功能-请参阅https://developer.wordpress.org/reference/functions/maybe_create_table/在调用此函数之前,必须手动包括upgrade.php
文件,否则将出现如下致命错误:
PHP致命错误:未捕获错误:调用未定义函数maybay\\u create\\u table()
我不知道这是怎么回事,也不知道为什么,但我能做到:
if (in_array(\'snippets\', $wpdb->tables)) {
// do something if wp_snippets exists
}
使用get_var
功能来自wpdb
使用异常处理初始化:
try {
$wpdb->hide_errors();
$wpdb->get_var( \'SELECT COUNT(*) FROM \' . $wpdb->prefix . \'translator\' );
$wpdb->show_errors();
} catch (Exception $e) {
error_log($e);
} finally {
translator_create_db();
}
参考号:SELECT a Variable我知道这是一个很老的问题,但也许有人会觉得它有用。处理新数据库、表版本上的函数并在不存在时创建的小计算器。
class ACCESS_TBDB {
private $wpdb = null;
private $accessTable = \'table_prefix\'; // just name of your table prefix
private $accessTableVer = \'1.0.7\'; // table version
private $accessTableOptName = \'access_db_ver\'; // name for option for db version
public function __construct()
{
global $wpdb;
//I does not like to call global $wpdb in every function in the class
$this->wpdb = $wpdb;
$tablename = $this->accessTable;
//set our table prefix
$this->wpdb->access_table = $this->wpdb->prefix . $tablename;
// every time check if db need to be created or not
$this->checkAccessDatabase();
}
private function createDBSQL(){
// create db sql for table
$charset = $this->wpdb->get_charset_collate();
$sql = "
CREATE TABLE {$this->wpdb->access_table} (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
name TINYTEXT,
type VARCHAR(10),
value SMALLINT DEFAULT 0 NOT NULL,
created_at DATETIME DEFAULT NULL,
PRIMARY KEY (id)
) $charset;
";
return $sql;
}
private function checkAccessDatabase(){
$version = $this->accessTableVer;
//handle DB versions
$db_version = get_option( $this->accessTableOptName , \'0.0.0\');
//Without we will face error on dbDelta and maybe_create_table
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
$dbCreateSql = $this->createDBSQL();
// check if maybe DB need upgrade
if (version_compare($version, $db_version, \'>\')) {
// if it contain old dev/legacy versions
if (version_compare($db_version, \'1.0.0\', \'<\')) {
$this->wpdb->query("DROP TABLE IF EXISTS {$this->wpdb->access_table};");
}
dbDelta($dbCreateSql);
update_option( $this->accessTableOptName , $version);
}else{
//https://developer.wordpress.org/reference/functions/maybe_create_table/
// just check DB if exist in case it was deleted because of reasons ... :)
$recreate = maybe_create_table( $this->wpdb->access_table, $dbCreateSql );
if(!$recreate){
update_option( $this->accessTableOptName , $version);
}
}
}
... other functions ...
}
我有一个运行WP 3.0.1的环境,其中有一个主数据库和两个从数据库。我正在使用HyperDB强制所有写入都转到主服务器,所有读取都从两个从服务器读取。我在wp admin页面中遇到了各种问题,其中数据正在写入主机,WordPress试图从从机读取,而数据尚未到达从机。这方面的一个例子是当我钩住\'dbx_post_advanced\' 在新帖子上预设一些类别和自定义分类术语。我已经验证,当我将HyperDB配置为仅从主机读写时,\'dbx_post_advanced\' 挂钩工作正常。我目前正在研究以下