$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>mysql - Getting Error Trying to Create Table|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

mysql - Getting Error Trying to Create Table

matteradmin9PV0评论

I'm trying to create a new table in phpmyadmin. I'm using MAMP and this is of course inside wordpress.

<?php
function testtheme_database() {
  global $wpdb;

  global $testtheme_db_version;
  $testtheme_db_version = "1.0";

  $table = $wpdb->prefix . "inquiries";

  $charset_collate = $wpdb->get_charset_collate();


  $sql = "CREATE TABLE $table (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    first-name varchar(50) NOT NULL,
    last-name varchar(50) NOT NULL,
    email varchar(50) DEFAULT '' NOT NULL,
    message longtext NOT NULL,
    PRIMARY KEY (id)
  ) $charset_collate; ";

  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  dbDelta($sql);
}
add_action( 'after_setup_theme', 'testtheme_database' );

I'm getting this error when I try to refresh/reload a certain page in wordpress, in my case it's the contact us page:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-name varchar(50) NOT NULL, last-name varchar(50) NOT NULL, email varcha' at line 3] CREATE TABLE wp_inquiries ( id mediumint(9) NOT NULL AUTO_INCREMENT, first-name varchar(50) NOT NULL, last-name varchar(50) NOT NULL, email varchar(50) DEFAULT '' NOT NULL, message longtext NOT NULL, PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci

It should create me a new table inside phpmyadmin with the name wp_inquiries but just getting that error and no table created.

I'm trying to create a new table in phpmyadmin. I'm using MAMP and this is of course inside wordpress.

<?php
function testtheme_database() {
  global $wpdb;

  global $testtheme_db_version;
  $testtheme_db_version = "1.0";

  $table = $wpdb->prefix . "inquiries";

  $charset_collate = $wpdb->get_charset_collate();


  $sql = "CREATE TABLE $table (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    first-name varchar(50) NOT NULL,
    last-name varchar(50) NOT NULL,
    email varchar(50) DEFAULT '' NOT NULL,
    message longtext NOT NULL,
    PRIMARY KEY (id)
  ) $charset_collate; ";

  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  dbDelta($sql);
}
add_action( 'after_setup_theme', 'testtheme_database' );

I'm getting this error when I try to refresh/reload a certain page in wordpress, in my case it's the contact us page:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-name varchar(50) NOT NULL, last-name varchar(50) NOT NULL, email varcha' at line 3] CREATE TABLE wp_inquiries ( id mediumint(9) NOT NULL AUTO_INCREMENT, first-name varchar(50) NOT NULL, last-name varchar(50) NOT NULL, email varchar(50) DEFAULT '' NOT NULL, message longtext NOT NULL, PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci

It should create me a new table inside phpmyadmin with the name wp_inquiries but just getting that error and no table created.

Share Improve this question edited Mar 5, 2019 at 5:38 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Mar 5, 2019 at 4:17 greenman0914greenman0914 112 bronze badges 1
  • 1 based on that error, I'll guess it has a problem with the hyphen in your column name – Milo Commented Mar 5, 2019 at 4:23
Add a comment  | 

1 Answer 1

Reset to default 1

From MySQL reference:

  • Permitted characters in unquoted identifiers:
    • ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
    • Extended: U+0080 .. U+FFFF
  • Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000:
    • ASCII: U+0001 .. U+007F
    • Extended: U+0080 .. U+FFFF
  • ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers.
  • Identifiers may begin with a digit but unless quoted may not consist solely of digits.
  • Database, table, and column names cannot end with space characters.

So you have to remove - characters from the names of columns or quote these names.

It’s worth to mention that it would be a good idea to use the same naming conventions that WP is using. So table names should be lower-case and use _ as separator.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far