简体   繁体   English

PHP MySQL数据库未创建新表

[英]PHP MySQL database not creating new table

So I want to create a new table in my database and I can't seem to get it working. 因此,我想在数据库中创建一个新表,但似乎无法正常工作。 It's a wordpress plugin so I don't know if that's what could be messing it up. 这是一个wordpress插件,所以我不知道那是不是搞砸了。

if ($wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . DB_PROFILE_TABLE . '"') != $wpdb->prefix . DB_PROFILE_TABLE)
        {
            $sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
                    id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
                    fname VARCHAR(100) NOT NULL,
                    sname VARCHAR(100),
                    city VARCHAR(100),
                    country VARCHAR(100)
                    CHARSET=utf8; ';
                dbDelta($sql);
        }

Beginner at php and mySql. php和mySql的初学者。

Looks like you are missing a closing parenthesis after country VARCHAR(100). 好像您在国家VARCHAR(100)之后缺少右括号。 You may also need to remove the semi-colon as well. 您可能还需要删除分号。

$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
    id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    fname VARCHAR(100) NOT NULL,
    sname VARCHAR(100),
    city VARCHAR(100),
    country VARCHAR(100))
    CHARSET=utf8';
dbDelta($sql);

You are missing parenthesis... 您缺少括号...

Can you try in this way 你可以这样尝试吗

global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  name tinytext NOT NULL,
  text text NOT NULL,
  url varchar(55) DEFAULT '' NOT NULL,
  UNIQUE KEY id (id)
) $charset_collate;";

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

These are the rules to create table using wordpress plugin 这些是使用wordpress plugin创建表格的规则

1) You must put each field on its own line in your SQL statement. 1)您必须在SQL语句中将每个字段放在自己的行上。

2) You must have two spaces between the words PRIMARY KEY and the definition of your primary key. 2)在单词PRIMARY KEY和主键定义之间必须有两个空格。

3) You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY. 3)您必须使用关键字KEY而不是其同义词INDEX,并且必须至少包含一个KEY。

4) You must not use any apostrophes or backticks around field names. 4)您不得在字段名称周围使用任何撇号或反引号。 Field types must be all lowercase. 字段类型必须全部为小写。

5) SQL keywords, like CREATE TABLE and UPDATE, must be uppercase. 5)SQL关键字(例如CREATE TABLE和UPDATE)必须为大写。

Refer http://codex.wordpress.org/Creating_Tables_with_Plugins 请参阅http://codex.wordpress.org/Creating_Tables_with_Plugins

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

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