-1

I'm just trying to get database creation/linking/editing working in PHP and I get this error when trying to add a table. Code included. I've tried running this in W3Schools' editor and I also get a syntax error there. Sorry if it's something obvious.

$sql = "CREATE TABLE myguest
(
FirstName int,
Email varchar(255),
PNumber varchar(255),
Price varchar(255),
Desc varchar(255)
);";    

"Error: CREATE TABLE myguest ( FirstName int, Email varchar(255), PNumber varchar(255), Price varchar(255), Desc varchar(255) );

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 'Desc varchar(255) )' at line 7"

I'm running MySQL 5.6.17 and PHP 5.5.12 on WAMP server.

TheCapeGreek
  • 107
  • 1
  • 2
  • 9
  • 3
    `desc` is a keyword in sql. You can not use it as a column name – Vamsi Prabhala Mar 29 '16 at 13:12
  • WHy not use `description` as column name? – Kisaragi Mar 29 '16 at 13:13
  • 1
    @vkp - Not true, you can use it as a column name or even a table name; you just have to wrap it in backticks so it's context is understood. – Geoff Atkins Mar 29 '16 at 13:17
  • 2
    Possible duplicate of [How do I escape reserved words used as column names? MySQL/Create Table](http://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table) – Geoff Atkins Mar 29 '16 at 13:22

1 Answers1

0

Desc is a reserved word in MySQL, just like trying to create a table called Table - it's possible, but you have to make sure MySQL understands the context.

Encapsulate it in backticks.

$sql = "CREATE TABLE myguest
(
`FirstName` int,
`Email` varchar(255),
`PNumber` varchar(255),
`Price` varchar(255),
`Desc` varchar(255)
);";  
Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23