Skip to content

Instantly share code, notes, and snippets.

@BretMishler
Forked from chales/db-connect-test.php
Last active May 19, 2016 16:59
Show Gist options
  • Save BretMishler/5f096411ec213b8288a0961593c22217 to your computer and use it in GitHub Desktop.
Save BretMishler/5f096411ec213b8288a0961593c22217 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 = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
$connect->select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = $connect->query($test_query);
$tblCnt = 0;
while($tbl = $result->fetch_array()) {
$tblCnt++;
#echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
echo "There are no tables<br />\n";
} else {
echo "There are $tblCnt tables<br />\n";
}
@BretMishler
Copy link
Author

BretMishler commented May 3, 2016

The source script used invoked several function calls that were "deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used."

@xgarb
Copy link

xgarb commented May 19, 2016

I think you are mixing procedural and object methods - http://www.w3schools.com/php/php_mysql_select.asp

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