简体   繁体   中英

What's wrong with this PHP-MySQL CREATE TABLE query?

First, I'm just starting to learn MySQL with PHP.

My query copy/paste directly from my IDE:

$query = "CREATE TABLE IF NOT EXISTS $table_messages (
                id       int(11)        unsigned  NOT NULL  auto_increment,
                show     tinyint(1)     unsigned  NOT NULL  default '0',
                to       varchar(255)             NOT NULL  default '',
                from     varchar(255)             NOT NULL  default '',
                type     varchar(255)             NOT NULL  default '',
                message  varchar(255)             NOT NULL  default '',
                PRIMARY KEY(id)
             ) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

$result = mysql_query( $query, $link ) OR exit ( mysql_error() );

Results in this error:

You have an error in your SQL syntax; near ' show tinyint(1) unsigned NOT NULL default '0' , to varchar(255) N' at line 4

... so I add one character to show (eg showz ) and get this error:

You have an error in your SQL syntax; near ' to varchar(255) NOT NULL default '' , from varchar(255) NOT NUL' at line 5

... so I add one character to to (eg toz ) and get this error:

You have an error in your SQL syntax; near ' from varchar(255) NOT NULL default '' , type varchar(255) NOT NU' at line 6

... so I add one character to from (eg fromz ) and IT WORKS!?

What is going on? Lol

If this question is too blatantly obvious, I'll remove it if the community thinks it would be prudent, but in the meantime I'm stumped.

BTW, I've messed with spacing, case and other things without any success.

SHOW , TO and FROM are reserved MySQL keywords. You must quote them with backticks to make them work as column names:

$query = "CREATE TABLE IF NOT EXISTS $table_messages (
    `id` int(11) unsigned NOT NULL auto_increment,
    `show` tinyint(1) unsigned NOT NULL default '0' ,
    `to` varchar(255) NOT NULL default '' ,
    `from` varchar(255) NOT NULL default '' ,
    `type` varchar(255) NOT NULL default '' ,
    `message` varchar(255) NOT NULL default '' ,
    PRIMARY KEY(id)
) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

It's usually good practice (though unneeded) to quote every column name this way to prevent accidental collisions with keywords as there are hundreds of them. For a full list, see http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html .

You might be interested in this list of reserved words in MySQL statements . In short, if you want to use any of these as a column name (or anywhere in following queries), you have to quote them, usually in backticks:

`show` TINYINT(1) UNSIGNED NOT NULL,

...and later:

SELECT `show` FROM `varchar` WHERE `to`="France"

Just a stab in the dark, but are to and from reserved words in mysql? Could you either wrap those words in [] like [to] and [from] or, like you did, change the terms to toperson or fromperson?

This is not the answer to your problem, but it's the answer to "What's wrong with a PHP-MySQL CREATE TABLE query?" (for another googler)

Maybe not all versions of PHP are like this, but in my environment, non PDO commands like "mysql_query" throws an error when I try to make a table:

CREATE TABLE IF NOT EXISTS `actionlog`

Error:

You have an error in your SQL syntax

Works just fine with the PDO adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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