Skip to content

Instantly share code, notes, and snippets.

@Be1zebub
Last active April 7, 2024 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Be1zebub/35726658cec2aef45454a33b29999e3a to your computer and use it in GitHub Desktop.
Save Be1zebub/35726658cec2aef45454a33b29999e3a to your computer and use it in GitHub Desktop.
Script that I use for testing sql connections
<?php
$TIMEOUT = 5; # in seconds
# cli args
# array_shift($argv);
# [$HOST, $DATABASE, $USER, $PASSWORD] = $argv;
# http GET query
# [$HOST, $DATABASE, $USER, $PASSWORD] = $_GET;
# Hardcode
$HOST = "1.1.1.1";
$DATABASE = "dbname";
$USER = "username";
$PASSWORD = "supersecret";
try {
$db = new PDO(
`mysql:host={$HOST};dbname={$DATABASE};charset=utf8`,
$USER,
$PASSWORD,
Array(
PDO::ATTR_TIMEOUT => $TIMEOUT,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
http_response_code(500);
die(`cant connect to db.\n ${$e->getMessage()}\n`);
} finally {
http_response_code(200);
die("success\n");
$smt = $db->query("SELECT table_name FROM information_schema.tables;");
print_r($smt->fetchAll(PDO::FETCH_COLUMN));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment