Skip to content

Instantly share code, notes, and snippets.

@klihelp
Created September 23, 2016 14:12
Show Gist options
  • Select an option

  • Save klihelp/d3a9d812e05f5179992033ccb99549a5 to your computer and use it in GitHub Desktop.

Select an option

Save klihelp/d3a9d812e05f5179992033ccb99549a5 to your computer and use it in GitHub Desktop.

Revisions

  1. klihelp created this gist Sep 23, 2016.
    34 changes: 34 additions & 0 deletions test_database_connect.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <?php

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'XXX');
    define('DB_USER', 'XXX');
    define('DB_PASSWORD', 'XXX');
    // Port : 3306

    $messages = array(
    'no_Host' => 'Unable to Connect to "'. DB_HOST.'".',
    'no_Name' => 'Could not open the db "'. DB_NAME.'".',
    'no_Table' => 'There are no tables.',
    );

    // Test HOST
    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die( $messages['no_Host'] );

    // Test Database Name
    mysqli_select_db($link, DB_NAME) or die( $messages['no_Name'] );

    // Check Nr of Tables
    $test_query = "SHOW TABLES FROM DB_NAME";
    $result = mysqli_query($link, $test_query);
    $nr_tables = 0;

    while( $table = mysqli_fetch_array($result) ) {
    $nr_tables++;
    }

    if ( ! $nr_tables ) {
    echo $messages['no_Table'];
    } else {
    echo "There are $nr_tables tables<br />\n";
    }