简体   繁体   English

此PHP-MySQL CREATE TABLE查询有什么问题?

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

First, I'm just starting to learn MySQL with PHP. 首先,我刚刚开始学习PHP的MySQL。

My query copy/paste directly from my IDE: 我的查询直接从我的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; 您的SQL语法有误; near ' show tinyint(1) unsigned NOT NULL default '0' , to varchar(255) N' at line 4 在' show tinyint(1)unsigned NOT NULL default'0'附近,到varchar(255)N'在第4行

... so I add one character to show (eg showz ) and get this error: ...所以我添加了一个字符来show (例如showz )并得到此错误:

You have an error in your SQL syntax; 您的SQL语法有误; near ' to varchar(255) NOT NULL default '' , from varchar(255) NOT NUL' at line 5 邻近' varchar(255)NOT NULL默认'在列5',从varchar(255)NOT NUL'

... so I add one character to to (eg toz ) and get this error: ...所以我to (例如toz )添加一个字符,并得到此错误:

You have an error in your SQL syntax; 您的SQL语法有误; near ' from varchar(255) NOT NULL default '' , type varchar(255) NOT NU' at line 6 靠近' from varchar(255)NOT NULL default'',在第6行键入varchar(255)NOT NU'

... so I add one character to from (eg fromz ) and IT WORKS!? ...因此,我向from添加了一个字符(例如fromz ),并且工作fromz

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. SHOWTOFROM是保留的MySQL关键字。 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 . 有关完整列表,请参见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 . 您可能对此MySQL语句中的保留字列表感兴趣。 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? 在黑暗中只是一个刺,但是在mysql中是保留字还是来回字? Could you either wrap those words in [] like [to] and [from] or, like you did, change the terms to toperson or fromperson? 您可以将这些词用[to]和[from]包裹在[]中,还是像您一样将术语改为toperson或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?" 这不是回答你的问题,但它是回答“什么是错 PHP,MySQL的CREATE TABLE查询?” (for another googler) (对于另一位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: 也许并非所有版本的PHP都是这样,但是在我的环境中,当尝试创建表时,非PDO命令(例如“ mysql_query”)会引发错误:

CREATE TABLE IF NOT EXISTS `actionlog`

Error: 错误:

You have an error in your SQL syntax

Works just fine with the PDO adapter. 在PDO适配器上可以正常工作。

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

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