Last active
January 12, 2017 12:35
-
-
Save ejsiddiqui/f41e52835297d2183afbc1b19368f364 to your computer and use it in GitHub Desktop.
PHP script to test if MySQL connection is working
This file contains 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 | |
// error handling. | |
ini_set ('display_errors', 1); | |
//error_reporting (E_ALL & ~E_NOTICE); | |
echo "Test Started....<br>"; | |
// -- Plese enter appropriate valeus --// | |
$dbhost = ''; //e.g. localhost | |
$dbuser = ''; //e.g. abc_ayz | |
$dbpass = ''; //e.g. ****** | |
$dbname = ''; //e.g. dbabc | |
$show_tables = "yes"; //values yes, no | |
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error()); | |
echo "Successfully Connected! <br>"; | |
echo "--------------------------------- <br>"; | |
echo "<strong>db host:</strong> ".$dbhost."<br>"; | |
echo "<strong>db User:</strong> ".$dbuser."<br>"; | |
echo "--------------------------------- <br>"; | |
/* Make sure the connection is still alive, if not, try to reconnect */ | |
if (mysql_ping($conn)) { | |
mysql_select_db($dbname) or die(mysql_error()); | |
echo "<strong>db</strong> ".$dbname ." selected successfully<br>"; | |
} else { | |
echo 'Lost connection, exiting now.'; | |
exit; | |
} | |
echo "---------------------------------"; | |
echo "<br>Querying database<br>"; | |
$query = "SHOW TABLES FROM $dbname"; | |
$result = mysql_query($query) or die(mysql_error()); | |
echo "Queryed database successfully<br>"; | |
echo "--------------------------------- <br>"; | |
if($show_tables=="yes"){ | |
$tblCnt = 0; | |
echo "<strong>Listing Tables</strong> <br>"; | |
echo "--------------------------------- <br>"; | |
while($tbl = mysql_fetch_array($result)) { | |
$tblCnt++; | |
echo $tbl[0]."<br />\n"; | |
} | |
if (!$tblCnt) { | |
echo "There are no tables<br />\n"; | |
} else { | |
echo "--------------------------------- <br>"; | |
echo "There are $tblCnt tables<br />\n"; | |
} | |
} | |
mysql_close(); // Close the connection. | |
echo "---------------------------------<br>"; | |
echo "------- <strong>Test Finished</strong> -------<br>"; | |
echo "---------------------------------"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment