0

I was trying to create a new table with the following PHP/mysql code snippet:

                    $query = "
                        CREATE TABLE IF NOT EXISTS :user (
                            id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                            username CHAR(24),
                            summonername VARCHAR(16),
                            password VARCHAR(16),
                            region CHAR(3),
                            lvl INT(2) DEFAULT '0',
                            maxlvl INT(2),
                            status VARCHAR(20),
                            enabled INT(1) DEFAULT '1',
                            priority INT(1) DEFAULT '0',
                            note VARCHAR(150)
                        )
                    ";

                    $query_params = array (
                        ':user' => $user,
                    );

If I execute this one, I get some syntax error:

Failed: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''testuser' ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, use' at line 1

It is weird because I checked the syntax according to the official documentation (http://dev.mysql.com/doc/refman/5.1/de/create-table.html) and also some user-made examples. Also, is there a way to set the default encoding of the table to utf8 (not for every single row, but global)?

I would be happy if someone can tell me how to fix the syntax errors there,

8
  • 2
    You can't use parameters for identifiers like table or column names. Commented Sep 16, 2014 at 16:36
  • as @VMai said, you can´t use identifiers on create table. But you have this solution: stackoverflow.com/questions/3665784/… Commented Sep 16, 2014 at 16:37
  • 2
    And I'm sure INT(1)/INT(2) don't mean whatever you think they mean! Commented Sep 16, 2014 at 16:54
  • INT(1) is a 1 character int...!? Like, a number with the length of 1 character. I'm aware that it is not 1 but can be 0-9 Commented Sep 16, 2014 at 16:57
  • 1
    It only really makes sense in the context of ZEROFILL. But seeing as padding can achieve exactly the same thing, it doesn't make a lot of sense there either. Commented Sep 16, 2014 at 17:27

3 Answers 3

1

You cannot use parameters for metadata. Nor should you be creating tables per user. Either add a field to hold the user, or sanitize the table name yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, so that's why it is throwing an error. But how can I create tables with variable names? // Because I kinda need to create tables for many different users. // I'm aware that this is not the correct way to store the users of the web site... They are stored in another (single) table obviously
Same way you replace variables into every other string in PHP; there's nothing special about SQL queries that prevents you from doing this.
0

With reference to my comment above, and by way of example...

 CREATE TABLE my_table(i INT(4) ZEROFILL NOT NULL PRIMARY KEY);

 INSERT INTO my_table VALUES (1),(9),(10),(99),(100),(999),(1000),(9999),(10000);

 SELECT * FROM my_table;
 +-------+
 | i     |
 +-------+
 |  0001 |
 |  0009 |
 |  0010 |
 |  0099 |
 |  0100 |
 |  0999 |
 |  1000 |
 |  9999 |
 | 10000 |
 +-------+

Comments

0

please try this query in create table

CREATE TABLE IF NOT EXISTS `user` (
                        id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                        username CHAR(24),
                        summonername VARCHAR(16),
                        password VARCHAR(16),
                        region CHAR(3),
                        lvl INT(2) DEFAULT '0',
                        maxlvl INT(2),
                        status VARCHAR(20),
                        enabled INT(1) DEFAULT '1',
                        priority INT(1) DEFAULT '0',
                        )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.