title: PDO no database selected
body: <p>I have created a login system for a website however I receive the following error message:</p>
  
  <blockquote>
    <p>Warning: PDOStatement::execute(): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\wamp\www\Bitev3\core\classes\users.php on line 18 when trying to log the user in.</p>
  </blockquote>
  
  <p>login php file</p>
  
  <pre><code>&lt;?php
  require 'core/init.php';
  $general-&gt;logged_in_protect();
  if (empty($_POST) === false) {
  
      $email = trim($_POST['email']);
      $password = trim($_POST['password']);
  
      if (empty($email) === true || empty($password) === true) {
          $errors[] = 'Sorry, but we need your email and password.';
      } else if ($users-&gt;email_exists($email) === false) {
          $errors[] = 'Sorry that email doesn\'t exists.';
      } else if ($users-&gt;email_confirmed($email) === false) {
          $errors[] = 'Sorry, but you need to activate your account. 
                       Please check your email.';
      } else {
  
          $login = $users-&gt;login($email, $password);
          if ($login === false) {
              $errors[] = 'Sorry, that email/password is invalid';
          }else {
              // email/password is correct and the login method of the $users object returns the user's id, which is stored in $login.
  
              $_SESSION['UserID'] =  $login; // The user's id is now set into the user's session  in the form of $_SESSION['id'] 
  
              #Redirect the user to home.php.
              header('Location: home.php');
              exit();
          }
      }
  } 
  ?&gt;
  &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;link rel="stylesheet" type="text/css" href="css/style.css" &gt;
      &lt;title&gt;Login&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;  
      &lt;div id="container"&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href="index.php"&gt;Home&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="register.php"&gt;Register&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="login.php"&gt;Login&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
          &lt;h1&gt;Login&lt;/h1&gt;
  
          &lt;?php if(empty($errors) === false){
  
              echo '&lt;p&gt;' . implode('&lt;/p&gt;&lt;p&gt;', $errors) . '&lt;/p&gt;';          
  
          } 
          ?&gt;
          &lt;form method="post" action=""&gt;
              &lt;h4&gt;Email:&lt;/h4&gt;
              &lt;input type="text" name="email"&gt;
              &lt;h4&gt;Password:&lt;/h4&gt;
              &lt;input type="password" name="password"&gt;
              &lt;br&gt;
              &lt;input type="submit" name="submit"&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>init php file</p>
  
  <pre><code>&lt;?php 
  #starting the users session
  session_start();
  require 'connect/database.php';
  require 'classes/users.php';
  require 'classes/general.php';
  
  $users      = new Users($db);
  $general    = new General();
  
  $errors     = array();
  </code></pre>
  
  <p>database php file </p>
  
  <pre><code>&lt;?php
  $config = array(
      'host' =&gt; 'locathost',
      'username' =&gt; 'root',
      'password' =&gt; 'pass123',
      'dbname' =&gt; 'bitev2'
  );
  
  $db =new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);
  
  $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  </code></pre>
  
  <p>general php file</p>
  
  <pre><code>class General{
  
  
  #Check if the user is logged in.
      public function logged_in () {
          return(isset($_SESSION['UserID'])) ? true : false;
      }
  
      #if logged in then redirect to home.php
      public function logged_in_protect() {
          if ($this-&gt;logged_in() === true) {
              header('Location: home.php');
              exit();     
          }
      }
  
      #if not logged in then redirect to index.php
      public function logged_out_protect() {
          if ($this-&gt;logged_in() === false) {
              header('Location: index.php');
              exit();
          }   
      }}
  </code></pre>
  
  <p>user php file</p>
  
  <pre><code>&lt;?php 
  class Users{
  
      private $db;
  
      public function __construct($database) {
          $this-&gt;db = $database;
      }
  
  
  public function email_exists($email) {
  
      $query = $this-&gt;db-&gt;prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email`= ?");
      $query-&gt;bindValue(1, $email);
  
      try{
  
          $query-&gt;execute();
          $rows = $query-&gt;fetchColumn();
  
          if($rows == 1){
              return true;
          }else{
              return false;
          }
  
      } catch (PDOException $e){
          die($e-&gt;getMessage());
      }
  
  }
  
  public function register($firstname, $lastname, $gender, $password, $email){
  
      $time       = time();
      $ip         = $_SERVER['REMOTE_ADDR'];
      $email_code = sha1($firstname + $lastname + microtime());
      $password   = sha1($password);
  
      $query  = $this-&gt;db-&gt;prepare("INSERT INTO `users` (`FirstName`, `LastName`, `Email`, `Password`, `Gender`, `ip`, `time`, `Email_code`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ");
  
      $query-&gt;bindValue(1, $firstname);
          $query-&gt;bindValue(2, $lastname);
      $query-&gt;bindValue(3, $email);
      $query-&gt;bindValue(4, $password);
      $query-&gt;bindValue(5, $gender);
      $query-&gt;bindValue(6, $ip);
          $query-&gt;bindValue(7, $time);
      $query-&gt;bindValue(8, $email_code);
  
      try{
          $query-&gt;execute();
  
  
      }catch(PDOException $e){
          die($e-&gt;getMessage());
      }   
  }
  public function activate($email, $email_code) {
  
          $query = $this-&gt;db-&gt;prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email` = ? AND `Email_code` = ? AND `confirmed` = ?");
  
          $query-&gt;bindValue(1, $email);
          $query-&gt;bindValue(2, $email_code);
          $query-&gt;bindValue(3, 0);
  
          try{
  
              $query-&gt;execute();
              $rows = $query-&gt;fetchColumn();
  
              if($rows == 1){
  
                  $query_2 = $this-&gt;db-&gt;prepare("UPDATE `users` SET `confirmed` = ? WHERE `Email` = ?");
  
                  $query_2-&gt;bindValue(1, 1);
                  $query_2-&gt;bindValue(2, $email);             
  
                  $query_2-&gt;execute();
                  return true;
  
              }else{
                  return false;
              }
  
          } catch(PDOException $e){
              die($e-&gt;getMessage());
          }
      }
  
          public function login($email, $password) {
  
      $query = $this-&gt;db-&gt;prepare("SELECT `Password`, `UserID` FROM `users` WHERE `Email` = ?");
      $query-&gt;bindValue(1, $email);
  
      try{
  
          $query-&gt;execute();
          $data               = $query-&gt;fetch();
          $stored_password    = $data['Password'];
          $id                 = $data['UserID'];
  
  
          if($stored_password === sha1($password)){
              return $id; 
          }else{
              return false;   
          }
  
      }catch(PDOException $e){
          die($e-&gt;getMessage());
      }
  }
  
  public function email_confirmed($email) {
  
      $query = $this-&gt;db-&gt;prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email`= ? AND `confirmed` = ?");
      $query-&gt;bindValue(1, $email);
      $query-&gt;bindValue(2, 1);
  
      try{
  
          $query-&gt;execute();
          $rows = $query-&gt;fetchColumn();
  
          if($rows == 1){
              return true;
          }else{
              return false;
          }
  
      } catch(PDOException $e){
          die($e-&gt;getMessage());
      }
  
  }
  public function userdata($id) {
  
      $query = $this-&gt;db-&gt;prepare("SELECT * FROM `users` WHERE `UserID`= ?");
      $query-&gt;bindValue(1, $id);
  
      try{
  
          $query-&gt;execute();
  
          return $query-&gt;fetch();
  
      } catch(PDOException $e){
  
          die($e-&gt;getMessage());
      }
  }
  public function get_users() {
  
  
      $query = $this-&gt;db-&gt;prepare("SELECT * FROM `users` ORDER BY `time` DESC");
  
      try{
          $query-&gt;execute();
      }catch(PDOException $e){
          die($e-&gt;getMessage());
      }
  
  
      return $query-&gt;fetchAll();
  }
  }
  </code></pre>