title: PHP: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
body: <p>I'm trying to make the registration page with PHP, but I'm new to this language and MySQL. So I've been following a tutorial about it. I followed everything correctly and checked multiple times. But I can't see the problem.
  config.php:</p>
  
  <pre><code>&lt;&lt;?php
  
  $db_user = "root";
  $db_pass = "";
  $db_name = "useraccounts";
  
  $db = new PDO('mysql:host=localhost;db_name=$db_name', $db_user, $db_pass);
  $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  ?&gt;
  </code></pre>
  
  <p>registration.php</p>
  
  <pre><code>&lt;?php
      if (isset($_POST['create'])) {
          if ($_POST['password'] == $_POST['confirm-password']) {
              $username = $_POST['username'];
              $email = $_POST['email'];
              $password = $_POST['password'];
  
              $sql = "INSERT INTO users (username, email, password) VALUES(?,?,?)";
              $stmtinsert = $db-&gt;prepare($sql);
  # line 30 \/ \/ \/ \/
              $result = $stmtinsert-&gt;execute([$username, $email, $password]);
              if ($result) {
                  echo "yeeeet";
              } else {
                  echo "nope";
              }
          }
      } else {
  
      }
      ?&gt;
  </code></pre>
  
  <p>Error:
  Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\xampp\htdocs\Website\registration.php:30 Stack trace: #0 C:\xampp\htdocs\Website\registration.php(30): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\Website\registration.php on line 30</p>