How to Connect MySql database using php in PDO ?

How to Connect MySql database using php in PDO ?

  • August 10, 2020
  • PHP

Connect MySql database using PHP in PDO with the following code:

    <?php
    define('DB_HOST', 'localhost');     // Host name
    define('DB_USER', 'root');         // DB User Name
    define('DB_PASS', '');             // DB User Password Name
    define('DB_NAME', 'demo');         // DB Name
    
    try {
        $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
    }
    catch (PDOException $e) {
        exit("Error: " . $e->getMessage());
    }
?>