Kerkouch
Kerkouch

Reputation: 1456

Mysql Error while selecting row

I got an error while trying to get a row from meta table

mysql> SHOW CREATE TABLE 'meta';
CREATE TABLE IF NOT EXISTS `meta` (
  `key` VARCHAR(255) NOT NULL,
  `value` LONGTEXT NOT NULL,
  UNIQUE INDEX `key_UNIQUE` (`key` ASC),
  PRIMARY KEY (`key`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;

mysql> SELECT * FROM 'meta';
+---------------------+-------------+
| key                 | value       |
+---------------------+-------------+
| website_title       | title       |
| website_description | description |
+---------------------+-------------+

and now when I execute this SQL query SELECT value FROM 'meta' WHERE key='website_title'; I got an error.

mysql> SELECT value FROM `meta` WHERE key='website_title';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual ...the right syntax to use near 'key='website_title'' at line 1

Upvotes: 1

Views: 47

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269663

key is a reserved word (think primary key, foreign key). It is best to choose a different name, but if you use it, then use backticks:

SELECT value
FROM `meta`
WHERE `key` = 'website_title';

A better approach is to change the name:

CREATE TABLE IF NOT EXISTS `meta` (
    meta_key VARCHAR(255) NOT NULL PRIMARY KEY.
    meta_value LONGTEXT NOT NULL
);

Building a unique index on a primary key is redundant. Don't bother.

Upvotes: 2

Alfabravo
Alfabravo

Reputation: 7569

key is a reserved word in MySQL. Change the column name.

Reference here.

Upvotes: 2

Related Questions