I am trying to create a MySQL Database using PHP by using the tutorial on W3Schools: https://www.w3schools.com/mysql/mysql_create_db.asp
However when I try to use this code to make a MySQL Database with the name of a variable I start having trouble. The variable is set via a cookie called Code, which is the users id on my site.
I have tried many different variations but nothing seems to work.
$code = $_COOKIE["Code"];
$servername = "localhost";
$username = "The Username Of My Database";
$password = "The Password Of My Database";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE '$code'";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
The error I keep getting is:
Error creating database: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''102159844'' at line 1
If you don't know what the number 10215988 is it's just the users ID.
I have tried many variations for the create line however they always bring up a similar error.
$sql = "CREATE DATABASE '$code'";
$sql = "CREATE DATABASE . $code";
$sql = "CREATE DATABASE " . $code;
If I tell it to echo $sql it outputs this:
CREATE DATABASE '102159844'
Thanks in advance for any help.