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.
Add this after your SQL querys – (It really helps and speeds up error correcting time)
echos this in your instance:
A MySQL error has occurred.
Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” . ‘
IDINT NOT NULL AUTO_INCREMENT PRIMARY KEY, ‘ . ‘pleaseVARCH’ at line 1This then indicates to me that the error regards “ & ‘.
After changing the code to contain single quotes and executing it, there is now no echo.
Note: Please refer to the MySQLi extension when using SQL embedded in PHP. mysql_* is in a deprecation process.
Hope this helps.