Created
September 23, 2016 14:12
-
-
Save klihelp/d3a9d812e05f5179992033ccb99549a5 to your computer and use it in GitHub Desktop.
Test script for database MySQL / MariaDB connection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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