Invalid table name, php / mysql
I have this code
mysqli_query ( $userdatabase,
'CREATE TABLE `user_'.$emailreg.'` (
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
IP varchar(10),
FLD1 varchar(20),
FLD2 varchar(40),
FLD3 varchar(25),
FLD4 varchar(25),
FLD5 varchar(25) )' );
echo ( mysqli_error ($userdatabase) );
which works fine on my localhost but when I upload it to the server it starts giving me the error "Invalid table name" user_vasya@rossya.pu ". any idea?
Thanks!
a source to share
As soon as you say, "I'm creating a table for the user," this is an anti-pattern that I call Tribbles Metadata . The problem is that these tables tend to get out of hand. :-)
Instead, you should create a single table with a column for the user's email as an attribute.
If you use delimited identifiers , SQL table names can contain special characters or even spaces, SQL reserved words, international characters, etc. But if you take back -quotes off, @
and .
are not valid characters for the identifier as it confuses the SQL parser. Think about it: Are you using any other programming languages ββthat accept .
as a valid character in a variable name or class name?
Other tips:
IP
easier to store as INT UNSIGNED
, and there are INET_ATON () and INET_NTOA () functions to convert an IP address string to and from a binary equivalent. This will help you make sure that you are not accidentally storing the wrong IP address. You can also use bitwise binary operators to perform IP subnet masking operations in binary form.
Columns FLD1
, FLD2
, FLD3
etc. are not descriptive and indicate that you did not sufficiently design this table. You may need to split this table into several separate tables to manage multivalued attributes. But there is no way to tell how to do this since your columns are named so abstractly.
a source to share
My first thought was "OMG! What a terrible name for a table!" :-)
The MySQL docs state that:
Names in the database and tables cannot contain "/", "\", "." or characters that are not allowed in file names.
I suspect you are violating this on two counts, .
in your domain name and (possibly) in the segment @
.
You should probably consider having one table that has an email address (or some other unique identifier).
a source to share