I'm pretty new to php.. and this is probably a stupid mistake... but I have no idea what is going on. I'm trying to create a table in my database using php. I want to name the table after the username. I'm using the variable $tableusername. Here's my code
$sql="SELECT * FROM userdata WHERE username='$username'";
$result=mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$tableusername = $row["username"];
}
$create = "CREATE TABLE `".$tableusername."` ('
. ' `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, '
. ' `please` VARCHAR(50) NOT NULL, '
. ' `make` VARCHAR(50) NOT NULL, '
. ' `this` VARCHAR(50) NOT NULL, '
. ' `work` VARCHAR(50) NOT NULL'
. ' )'
. ' ENGINE = myisam;";
mysql_query($create)
?>
<html>
<head>
</head>
<body>
You have successfully signed up. <?php echo $tableusername ?>
</body>
</html>
So- This creates a table named $tableusername. The variable doesn't carry over. BUT- when I echo $tableusername - the variable carries over. I'm pretty new to this - so any help is appreciated.
Thank you!
mysql_*
functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is good tutorial. Also see Why shouldn't I usemysql
functions in PHP? – Madara Uchiha Oct 21 '12 at 17:30$username
and$tableusername
are the same according to your code :) – Alexander Oct 21 '12 at 17:32