Skip to content

Instantly share code, notes, and snippets.

@robertopc
Created April 11, 2023 20:09
Show Gist options
  • Save robertopc/649388e23de26534335d5b7d0fce1625 to your computer and use it in GitHub Desktop.
Save robertopc/649388e23de26534335d5b7d0fce1625 to your computer and use it in GitHub Desktop.
Test PostgreSQL and MySQL/MariaDB connectivity with PHP
<?php
$servername = "localhost";
$username = "user";
$password = "password";
echo"Postgre connection test<br>";
try {
$conn = new PDO("pgsql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
echo "<hr>Mariadb connection test<br>";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment