简体   繁体   English

MySQL“ CREATE TABLE”查询错误

[英]Error with MySQL “CREATE TABLE” query

I know that this has been asked hundreds of times before, but I looked around the Internet and couldn't find what could possibly be wrong with my syntax: 我知道这已经被问过数百遍了,但是我环顾了整个互联网,找不到语法可能有什么问题:

mysql_select_db('Projects');

        $sql = "CREATE TABLE $data[ID] (
            ID INT NOT NULL,
            Creator INT NOT NULL,
            Name VARCHAR(20) NOT NULL,
            Version VARCHAR(20) NOT NULL,
            Status VARCHAR(20) NOT NULL,
            Date VARCHAR(20) NOT NULL,
            Skript VARCHAR(20) NOT NULL,
            Filename VARCHAR(20) NOT NULL,
            Downloads INT NOT NULL,
            PRIMARY KEY(ID)
            )";

        if (mysql_query($sql)){

            echo "Created";

        }else{

            echo "Not created, ".mysql_error();

        }

I tried using backticks, quotation marks et.c but in vain. 我尝试使用反引号,引号等,但没有成功。 The table name (21) displayed in the error is correct. 错误中显示的表名(21)是正确的。

Here's the error that I get, including the echo "Not created, ";: 这是我得到的错误,包括回显“未创建” ;;:

Not created, You have an error in your SQL syntax; 未创建,您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '23 ( ID INT NOT NULL, Creator INT NOT NULL, Name VARCHAR(20) NOT NUL' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行附近使用'23(ID INT NOT NULL,Creator INT NOT NULL,Name VARCHAR(20)NOT NUL'

Rules for naming objects, including tables in MySql: 命名对象的规则,包括MySql中的表:

http://dev.mysql.com/doc/refman/5.1/en/identifiers.html http://dev.mysql.com/doc/refman/5.1/zh-CN/identifiers.html

Identifiers may begin with a digit but unless quoted may not consist solely of digits. 标识符可以以数字开头,但除非加引号,否则不能仅由数字组成。

you cant name your table start with digits 您不能用数字开头表名

this will work forexample 这将工作例如

   $sql = "CREATE TABLE 't'.$data[ID] (
        ID INT NOT NULL,
        Creator INT NOT NULL,
        Name VARCHAR(20) NOT NULL,
        Version VARCHAR(20) NOT NULL,
        Status VARCHAR(20) NOT NULL,
        Date VARCHAR(20) NOT NULL,
        Skript VARCHAR(20) NOT NULL,
        Filename VARCHAR(20) NOT NULL,
        Downloads INT NOT NULL,
        PRIMARY KEY(ID)
        )";

as you see it starts by t 如您所见,它从t开始

or use backticks around it. 或在其周围使用反引号。 like that 像那样

   `$data[ID]`

Your query seems correct except the value of $data[ID]. 您的查询似乎正确,除了$ data [ID]的值。 You should check it. 您应该检查一下。

I think the problem is with the value of the $data[ID] what is the value of it? 我认为问题在于$ data [ID]的值是什么? Also, use mysqli_query() or PDO (as indicated by bestprogrammerintheworld) instead of the old mysql_query(). 另外,请使用mysqli_query()或PDO(如世界上的bestprogrammer所示),而不要使用旧的mysql_query()。

You've managed to omit the most important piece of info, which is the SQL code that triggers the SQL error. 您已经设法省略了最重要的信息,即触发SQL错误的SQL代码。 But we can gather some hints from the PHP that generates it: 但是我们可以从生成它的PHP中收集一些提示:

$sql = "CREATE TABLE $data[ID] (
            ID INT NOT NULL,

... and the error message: ...以及错误消息:

near '23 ( ID INT NOT NULL '23附近(ID INT NOT NULL

I suspect you try to run this SQL: 我怀疑您尝试运行此SQL:

CREATE TABLE 23 (
    ID INT NOT NULL

If you actually want to create a table called 23 you need to quote it with backticks every single time you make use of it, including creation: 如果您实际上想创建一个名为23的表,则每次使用它时都需要用反引号将其引起来,包括创建:

CREATE TABLE `23` (
    ID INT NOT NULL

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

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