Skip to content

Instantly share code, notes, and snippets.

@mattstein
Last active December 19, 2024 09:13

Revisions

  1. Matt Stein revised this gist Oct 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion db-test.php
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@
    or die("🚫 Unable to Connect to '$host'.\n\n");
    mysqli_select_db($connection, $database)
    or die("🚫 Connected but could not open db '$database'.\n\n");
    $result = mysqli_query($connection, "SHOW TABLES FROM $database");
    $result = mysqli_query($connection, "SHOW TABLES FROM `$database`");

    if ($result === false) {
    die("🚫 Couldn’t query '$database'.\n\n");
  2. mattstein created this gist Aug 28, 2020.
    75 changes: 75 additions & 0 deletions db-test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    <?php

    /**
    * Database Connection Tester
    * A quick-and-dirty standalone script for checking PHP’s connection to a
    * MySQL (or MariaDB) or PostgreSQL database.
    *
    * To use, enter the settings below and run the following from your terminal:
    * ```
    * php -f db-test.php
    * ```
    */

    $driver = 'mysql'; // 'mysql' or 'pgsql'
    $database = '';
    $username = '';
    $password = '';
    $host = '';
    $port = $driver === 'mysql' ? 3306 : 5432;

    /**
    * Ignore the stuff below this line.
    */
    $numTables = 0;

    echo "------------------------------------------------\n";
    echo "Database Connection Test\n";
    echo "PHP ".PHP_VERSION."\n";
    echo "DB driver: $driver\n";
    echo "------------------------------------------------\n";

    if ($driver === 'mysql') {
    $connection = mysqli_connect($host, $username, $password, null, $port)
    or die("🚫 Unable to Connect to '$host'.\n\n");
    mysqli_select_db($connection, $database)
    or die("🚫 Connected but could not open db '$database'.\n\n");
    $result = mysqli_query($connection, "SHOW TABLES FROM $database");

    if ($result === false) {
    die("🚫 Couldn’t query '$database'.\n\n");
    }

    while($table = mysqli_fetch_array($result)) {
    $numTables++;
    //echo $table[0]."\n";
    }
    } elseif ($driver === 'pgsql') {
    $connection = pg_connect("host=$host dbname=$database user=$username password=$password port=$port")
    or die("🚫 Unable to Connect to '$host'.\n\n");
    $result = pg_query(
    $connection,
    "SELECT table_schema || '.' || table_name
    FROM information_schema.tables
    WHERE table_type = 'BASE TABLE'
    AND table_schema NOT IN ('pg_catalog', 'information_schema');
    "
    );

    if ($result === false) {
    die("🚫 Couldn’t query '$database'.\n\n");
    }

    while($table = pg_fetch_array($result)) {
    $numTables++;
    //echo $table[0]."\n";
    }
    } else {
    die("⛔ Invalid driver `$driver`; must be `mysql` or `pgsql`.\n\n");
    }

    if (!$numTables) {
    echo "🤷‍️ Connected but no tables found.\n\n";
    } else {
    echo "👍 Connected and found $numTables tables.\n\n";
    }