Create a gist now

Instantly share code, notes, and snippets.

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";
}
@chrisdrogaris

Says no tables when there are some.

@aeroson
aeroson commented Sep 13, 2015

thank you

@joaoBeno

Thanks!

@changlee2

That really worked perfect and it is fantastic! I tried so many scripts, and that worked excellent. Many thanks.

@lexblagus

Great! Obrigado!

@abhinov2

thanks

@ron4stoppable

great for fast implementation

@positivecharge8

I forked this to make some edits to deal with deprecated functions but I'm not sure how to push it back to yours for approval if you so choose to commit the changes.

@stephangriesel

thank you! works great.

@omalave
omalave commented Jun 13, 2016

mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

@ChimpGamer

gives me 403 error

@ultrablue

Nice. Simple, useful, direct and to the point. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment