Skip to content

Instantly share code, notes, and snippets.

@janson
Forked from chales/db-connect-test.php
Last active December 6, 2016 15:33
Show Gist options
  • Save janson/f41b177517737142184ed6875801c455 to your computer and use it in GitHub Desktop.
Save janson/f41b177517737142184ed6875801c455 to your computer and use it in GitHub Desktop.
Script for a quick PHP MySQL DB connection test.
<?php
# Fill out the four db vars and run on cli
# $ php -f db-mysql_connect-test.php
$dbname = 'db';
$dbuser = 'user';
$dbpass = '';
$dbhost = 'localhost';
## end vars
$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++;
}
if (!$tblCnt) {
echo "There are no tables in $dbname\n";
} else {
echo "There are $tblCnt tables for $dbname\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment