繁体   English   中英

使用dbDelta函数在Wordpress中创建表

[英]Creating Table in Wordpress with dbDelta function

我需要在wordpress中为插件创建一个自定义表。 我按照了一些在线文本并创建了表格,但发现它无法访问。 当尝试从表中选择所有内容时,不会返回任何内容,当尝试使用数据库浏览器插件查看表时,我收到此错误:“您的SQL语法中有错误;请查看与您的MySQL服务器对应的手册在第1行“FROM wp-typeEvents LIMIT 0,100”附近使用正确语法的版本“以响应插件的查询(”SELECT SQL_CALC_FOUND_ROWS FROM wp-typeEvents LIMIT 0,100 ;;“)。

简而言之,我正在尝试使用dbDelta来创建表。 该表已创建,但存在某种问题,导致无法添加行或读取其内容。

我已经读过dbDelta可以是finnicky函数,所以我试图坚持它的三个黄金规则:

- 在新线上放置每个字段
- 在主键及其定义之间放置两个空格
- 至少有一把钥匙

这是代码:

global $wpdb;

$tablename = "wp-typeEvents";   
$query = "CREATE TABLE `" . $tablename . "` (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `eventName` varchar(60) NOT NULL,
    `location` varchar(60) DEFAULT '' NULL,
    `price` double NOT NULL,
    `description` text NOT NULL,
    `paypal` varchar(60) NOT NULL,
    PRIMARY KEY  (`id`)
    );";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($query);

有任何想法吗?

不要对表名进行硬编码,使用$ wpdb-> prefix(如别人所说)。

不要在字段名称周围使用引号。

还有其他有用的“规则”,它们都列在这里: http//codex.wordpress.org/Creating_Tables_with_Plugins

但请注意,dbDelta函数相当挑剔。 例如:

  • 您必须在SQL语句中将每个字段放在自己的行中。
  • 在PRIMARY KEY和主键的定义之间必须有两个空格。
  • 您必须使用关键字KEY而不是其同义词INDEX,并且您必须至少包含一个KEY。
  • 您不得在字段名称周围使用任何撇号或反引号。
  • 字段类型必须全部小写。
  • SQL关键字(如CREATE TABLE和UPDATE)必须为大写。

如果它在插件中,则require_once()路径是错误的。 应该:

require_once('/includes/upgrade.php');

应该更正能够加载dbDelta()函数。

希望这可以帮助。

另请注意,当前的dbDelta()函数不解析它所引用的列的UNIQUE KEY名称上的排序关键字ASC | DESC,它会将约束添加到剩余的索引中,因此它会尝试添加约束触发唯一键错误。 请参阅以下示例:

尝试创建此问题textry将触发错误。

CREATE TABLE wp_test (
        id int(11) NOT NULL AUTO_INCREMENT,
        email varchar(100) NOT NULL,
        PRIMARY KEY  (stcr_id),
        UNIQUE KEY uk_email (subscriber_email ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET utf8;

默认情况下,订购类型是ASC,因此摆脱它将起作用。

CREATE TABLE wp_test (
        id int(11) NOT NULL AUTO_INCREMENT,
        email varchar(100) NOT NULL,
        PRIMARY KEY  (stcr_id),
        UNIQUE KEY uk_email (subscriber_email))
ENGINE = InnoDB
DEFAULT CHARACTER SET utf8;

我在WordPress 4.1.1上测试了这个

在使用wordpress的dbDelta核心功能时,我遇到了一些问题,并决定为它创建一个函数:

/**
 * Prevents unnecessary re-creating index and repetitive altering table operations when using WordPress dbDelta function
 *
 * Usage Example:
 *
 * $table_name      = "ratings";
 *
 * $table_columns   = "id INT(6) UNSIGNED AUTO_INCREMENT,
 *                  rate tinyint(1) NOT NULL,
 *                  ticket_id bigint(20) NOT NULL,
 *                  response_id bigint(20) NOT NULL,
 *                  created_at TIMESTAMP";
 *
 * $table_keys      = "PRIMARY KEY (id),
 *                  KEY ratings_rate (rate),
 *                  UNIQUE KEY ratings_response_id (response_id)";
 *
 * create_table($table_name, $table_columns, $table_keys);
 *
 * Things that need to be considered when using dbDelta function :
 *
 * You must put each field on its own line in your SQL statement.
 * You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
 * You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
 * You must not use any apostrophes or backticks around field names.
 * Field types must be all lowercase.
 * SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
 * You must specify the length of all fields that accept a length parameter. int(11), for example.
 *
 * Further information can be found on here:
 *
 * http://codex.wordpress.org/Creating_Tables_with_Plugins
 *
 * @param $table_name
 * @param $table_columns
 * @param null $table_keys
 * @param null $charset_collate
 * @version 1.0.1
 * @author Ugur Mirza Zeyrek
 */
function create_table($table_name, $table_columns, $table_keys = null, $db_prefix = true, $charset_collate = null) {
    global $wpdb;

    if($charset_collate == null)
        $charset_collate = $wpdb->get_charset_collate();
    $table_name = ($db_prefix) ? $wpdb->prefix.$table_name : $table_name;
    $table_columns = strtolower($table_columns);

    if($table_keys)
        $table_keys =  ", $table_keys";

    $table_structure = "( $table_columns $table_keys )";

    $search_array = array();
    $replace_array = array();

    $search_array[] = "`";
    $replace_array[] = "";

    $table_structure = str_replace($search_array,$replace_array,$table_structure);

    $sql = "CREATE TABLE $table_name $table_structure $charset_collate;";

    // Rather than executing an SQL query directly, we'll use the dbDelta function in wp-admin/includes/upgrade.php (we'll have to load this file, as it is not loaded by default)
    require_once (ABSPATH . 'wp-admin/includes/upgrade.php');

    // The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary
    return dbDelta($sql);
}

https://github.com/mirzazeyrek/wordpress_create_table

除了您自己发现的问题之外,您似乎对表前缀进行了硬编码。 你不应该这样做。 相反,根据手册,你应该使用: -

$wpdb->prefix

这将存储前缀,并应添加到表名之前。 这允许改变前缀的可能性。

http://codex.wordpress.org/Creating_Tables_with_Plugins

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM