Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

php - Creating an SQL table with email address as title

I'm struggling to create SQL tables using email addresses as the table name.

The code I am using produces the following error:

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 '@gmail.com( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR' at line 1

and the related code is:

// create new table for users tosses
    // Make a MySQL Connection
        mysql_connect("localhost", "$sqlusername", "$sqlpassword") or die(mysql_error());
        mysql_select_db("$db_name") or die(mysql_error());

// Create a MySQL table in the selected database
        mysql_query("CREATE TABLE $emailaddress(
        id INT NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(id),
        name VARCHAR(30), 
        age INT)")
        or die(mysql_error());  

It works just fine when I input a value from the variable $emailaddress that is not an email address i.e. doesnt have an @ symbol, but it shows that error if it includes an @ symbol.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be able to get around this by escaping the table name with backticks (`). But really for most circumstances this is a bad way to design your database.

You should use one table with an emailaddress field instead of creating a new table for each user/email address, unless you have a specific need to do so.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...