Skip to content

Instantly share code, notes, and snippets.

@kamleshwebtech
Forked from chales/db-connect-test.php
Created May 4, 2021 13:49
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 kamleshwebtech/38ddf7e6e90a5b5b232bd0a79576d44f to your computer and use it in GitHub Desktop.
Save kamleshwebtech/38ddf7e6e90a5b5b232bd0a79576d44f to your computer and use it in GitHub Desktop.
Script for a quick PHP MySQL DB connection test.
<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
$tblCnt++;
#echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
echo "There are no tables<br />\n";
} else {
echo "There are $tblCnt tables<br />\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment