create database tables in mysql using data from variable

How does one use the data from a variable to create a table with the same name?

If there is a variable called $user_data and we accessed the ['username'] value how can this be used to create a database table with the same value, so that each user can have their own table in the database, and it is named the same as their username in the database.

Is this a correct way to link tables or what is the best way to layout database data. And how are the tables and databases connected via shared data?

Answers


Something along these lines:

mysql_query("CREATE TABLE {$user_data['username']}(id INT NOT NULL AUTO_INCREMENT);  

You can query a table creation.

mysql_connect("localhost", "bill", "3pD]gks*dg");
mysql_select_db("user_db");
mysql_query("CREATE TABLE ".$user_data['username']."(some_text varchar(15), some_int int)");

Within the parenthesis of the query, you should place your desired table structure.


This is a really bad idea. There is actually whole chapter "Metadata Tribbles" on this subject in SQL Antipatterns book (btw, my recommendation, get it).

First of all, in the case of users, this will open you application for additional injection threats. Both simple one and and second order sql injections (read bout here and here). And this will reduce you ability to index data.

Instead you should keep all user details in one (or two, if it a heavy use site) table, and utilize foreign key to link users to other content which then have made.


Need Your Help

Python unpickling error when using base64encoding

python base64 pickle

I am trying to use pickling and I can't. It seems that I am doing something wrong. What is it? (using python 2.7)

Recommendations on developing a WPF application without using MVVM or similar

wpf recommendation-engine

We were building out the next version of an in-house thick-client application using WPF/Prism (Composite Application Library). As we were nearly done with the client our team was put under new