Hello,

I am stucked while making code in PHP.
I actually want to make a table in database with name as username (of the respective user).
(table should be formed only when no such table exisit before)

I used session to get username and every thing is right except formation of table... help me

session_start();
$tabname =  $_SESSION['username'];

$s1 =  sprintf("select * from '$tabname'" );
$result = mysql_query($s1);

if(!$result) 
{
$s2 = "CREATE TABLE '$tabname' (ID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID),input varchar(50))";
mysql_query( $s2 );
}

Try this.

session_start();
$tabname =  $_SESSION['username'];

$s1 =  sprintf("select * from '$tabname'" );
$result = mysql_query($s1);

if(!$result) 
{
$s2 = "CREATE TABLE '"$tabname"'(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), input VARCHAR(50))" or die(mysql_error());
mysql_query( $s2 );
}
Member Avatar for diafol

Not trying to be awkward, but do you really need to create a table for every user? 99.99% of the time, there is a far better solution. A users table should suffice with related tables for specific user data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.