Skip to content

Instantly share code, notes, and snippets.

@Humoud
Forked from chales/db-connect-test.php
Last active March 7, 2016 11:42
Show Gist options
  • Save Humoud/29643d4fffbbd2668c9a to your computer and use it in GitHub Desktop.
Save Humoud/29643d4fffbbd2668c9a to your computer and use it in GitHub Desktop.
Script for a quick PHP MySQL DB connection test.
<?php
###
# NOTE: This code won't work with PHP 7.
# MySQL is deprecated and will be removed in PHP 7 checkout MySQLi.
###
############## JUST FILL THESE VARIABLES ######
$dbname = 'name'; # Name of the Database in the MySQL Server
$dbuser = 'user'; # User name of the database in MySQL
$dbpass = 'pass'; # Password for the user of in database in MySQL
$dbhost = 'host'; # url/link to database
################################################
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
echo "<p>Connected to MySQL Database Server successfuly.</p>";
mysql_select_db($dbname) or die("Could not open the databse in the MySQL Server '$dbname'");
echo "<p>Selected a Database in the Server successfuly.</p>";
$test_query = "SHOW TABLES FROM $dbname";
echo "<p>Ran Query: SHOW TABLES</p>";
$result = mysql_query($test_query);
echo "<p>Printing Results:</p>";
$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