Testing PDO Database Connection on Hosting
Let's now test the connection to the database via the PDO extension using the following test code:
<?php
$host = ''; // host name
$name = ''; // database name
$user = ''; // username
$pass = ''; // password
$charset = 'utf8';
$dsn = "mysql:host=$host; dbname=$name; charset=$charset";
$opt = [
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, $opt);
echo 'DB is connected';
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Substitute your credentials into the test code, then upload this code to your site and test the connection to the database.