I am creating dynamic tables in mysql database using the following code which works fine.
however, I want to create a second table as well based on the dynamic table name!
for example:
the dynamic table name
is : David
the second table based on David table
should be: David.books
or David-books
here is my current code:
$stmt = mysqli_prepare( $db_conx, "CREATE TABLE $user (
id int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)"
);
I tried this and didn't create the table:
$stmt = mysqli_prepare( $db_conx, "CREATE TABLE $user.'books' (
id int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)"
);
I also tried it like this and it did not create any tables:
$stmt = mysqli_prepare( $db_conx, "CREATE TABLE $user.books (
id int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)"
);
could someone please help me out?
Thanks
You shouldn't single-quote around your table name:
$stmt = mysqli_prepare( $db_conx, "CREATE TABLE " .$user. "_books (
id int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)"
);
Also, don't use a dot in a table name..
Also, per @Fred-ii-, don't use a dash in your table names.
$stmt = mysqli_prepare( $db_conx, "CREATE TABLE ".$user."-books ( id int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) )" );
You cannot have dots in mySQL table names, with unquoted identifiers.
Database and table names cannot contain '/', '\', '.', or characters
that are not permitted in file names.
ref : http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
Moreover, your use of tables seems unorthodox and you should use the possibilities offered by RELATIONAL databases.
Such as : Database schema for Books, Authors, Publishers and Users with bookshelves
The correct way of putting strings together is using a . like you did, but you did not define the dot that you want in the string itself:
$string = $user . ".books"; // This will return "David.books"
$string = $user . "-books"; // And this will return "David-books"
You see how I added an additional dot?
Perhaps you could even do something like $string = $user.".books";
and then doing
$stmt = mysqli_prepare($db_conx, "CREATE TABLE $string");
Because I don't think it MySQL allows you to put strings together like the way you did
you cant write $user as-is. you have to do like this: "CREATE TABLE '".$user.".books' ( id in(11) .... hope it helps