Skip to content

Instantly share code, notes, and snippets.

@klihelp
Created September 23, 2016 14:12
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 klihelp/d3a9d812e05f5179992033ccb99549a5 to your computer and use it in GitHub Desktop.
Save klihelp/d3a9d812e05f5179992033ccb99549a5 to your computer and use it in GitHub Desktop.
Test script for database MySQL / MariaDB connection.
<?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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment