PDO database connection

PHOTO EMBED

Fri Nov 29 2024 18:53:42 GMT+0000 (Coordinated Universal Time)

Saved by @Joe_Devs #php

<?php
//Database credentials
$host = 'localhost';
$port = 3306;
$dbName = 'blog';
$username = 'root';
$password = '';

$dsn = "mysql:host={$host};dbname={$dbName};port={$port};charset=utf8";

try {
    // create PDO instance
    $pdo = new PDO($dsn, $username, $password);

//    set PDO to throw exceptions on error
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//    Fetch as associative array
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

} catch (PDOException $e) {
// if error catch
    echo "Connection Failed {$e->getMessage()}";
}
content_copyCOPY

just change the $host $port $dbName, $username, and $password as needed and it should work for. *once up and running delete the echo that says database connected in line 18*