Skip to content

Instantly share code, notes, and snippets.

@s3rgeym
Last active February 11, 2025 03:38
Show Gist options
  • Save s3rgeym/7a963d9cb9d1cf9a5637740d592330af to your computer and use it in GitHub Desktop.
Save s3rgeym/7a963d9cb9d1cf9a5637740d592330af to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
if ($argc < 5) {
echo "Usage: ./check_db_connection.php <host> <username> <password> <dbname> [port]\n";
exit(1);
}
$db_host = $argv[1];
$db_username = $argv[2];
$db_password = $argv[3];
$db_name = $argv[4];
$db_port = isset($argv[5]) ? $argv[5] : ''; // Port is optional
try {
$dsn = "mysql:host=$db_host;dbname=$db_name" . ($db_port ? ";port=$db_port" : "");
$conn = new PDO($dsn, $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Database connection successful!\n";
} catch (PDOException $e) {
echo "Database connection failed: " . $e->getMessage() . "\n";
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment