bubble_chart StackLab

CRUD Web Application Using Php Data Objects

5 steps · pending Not started · person_outline Guest
arrow_back Back

Configure the Database Connection

On the Php code add the following database connection script to check your existing database on the MySQL server

code php
//DATABASE CONNECTION
$host = 'localhost';
$db   = 'it30_lab_db';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";

$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}
You can add echo scripts to on the try catch block to check if the connection had succeed or failed.

login Sign in to save your progress permanently. info Progress is saved in your browser session
format_list_numbered Step 3 of 5