title: PHP using variables to create a MySQL Database
body: <p>I am trying to create a MySQL Database using PHP by using the tutorial on W3Schools: <a href="https://www.w3schools.com/mysql/mysql_create_db.asp" rel="nofollow noreferrer">https://www.w3schools.com/mysql/mysql_create_db.asp</a></p>
  <p>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.</p>
  <p>I have tried many different variations but nothing seems to work.</p>
  <pre><code>$code = $_COOKIE[&quot;Code&quot;];
  
  $servername = &quot;localhost&quot;;
  $username = &quot;The Username Of My Database&quot;;
  $password = &quot;The Password Of My Database&quot;;
  
  // Create connection
  $conn = new mysqli($servername, $username, $password);
  // Check connection
  if ($conn-&gt;connect_error) {
      die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);
  }
  
  // Create database
  $sql = &quot;CREATE DATABASE '$code'&quot;;
  if ($conn-&gt;query($sql) === TRUE) {
      echo &quot;Database created successfully&quot;;
  } else {
      echo &quot;Error creating database: &quot; . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  </code></pre>
  <p>The error I keep getting is:</p>
  <blockquote>
  <p>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</p>
  </blockquote>
  <p>If you don't know what the number 10215988 is it's just the users ID.</p>
  <p>I have tried many variations for the create line however they always bring up a similar error.</p>
  <pre><code>$sql = &quot;CREATE DATABASE '$code'&quot;;
  $sql = &quot;CREATE DATABASE . $code&quot;;
  $sql = &quot;CREATE DATABASE &quot; . $code;
  </code></pre>
  <p>If I tell it to echo $sql it outputs this:</p>
  <blockquote>
  <p>CREATE DATABASE '102159844'</p>
  </blockquote>
  <p>Thanks in advance for any help.</p>