Skorpius

Php/PDO Connection

May 25th, 2022 (edited)
566
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     $DB_HOST  = 'localhost';
  4.     $DB_LOGIN = 'root';
  5.     $DB_PASS  = 'password_here';
  6.     $DB_NAME  = 'Database_Name_Here';
  7.     $charset  = 'utf8mb4';
  8.  
  9.     $options = [
  10.         \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
  11.         \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  12.         \PDO::ATTR_EMULATE_PREPARES => false,
  13.     ];
  14.  
  15.     $dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$charset";
  16.  
  17.     try{
  18.  
  19.         $pdo = new PDO($dsn, $DB_LOGIN,$DB_PASS,$options);
  20.  
  21.         $connected = "You are successfully connected to the ".$DB_NAME." database";
  22.         return $connected; //To Test echo $connected on home page to show connection
  23.  
  24.     }catch(PDOException $e){
  25.  
  26.         die ('Connection Failed: '.$e->getMessage());
  27.  
  28.     }
  29. ?>
RAW Paste Data Copied