Skip to content

Instantly share code, notes, and snippets.

@gayanvirajith
Last active August 29, 2015 14:04
Show Gist options
  • Save gayanvirajith/7b1aaafa180748a2934b to your computer and use it in GitHub Desktop.
Save gayanvirajith/7b1aaafa180748a2934b to your computer and use it in GitHub Desktop.
Check MySQL db connection in php
<?php
/**
* Checking the MySQL connection
*/
// Host name
$host = "localhost";
// MySQL username
$username = "databaseusername";
// MySQL password
$password = "password";
// MySQL database name
$databasename = "dbname";
echo "Checking connection with {$host}<br/>";
// ------------------------------
// Connect to mysql with the host, username, password
// ------------------------------
$con = mysql_connect($host,$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// ------------------------------
// Select database connection
// ------------------------------
$db_selected = mysql_select_db($databasename, $con);
if (!$db_selected) die ("Can\'t use test_db : " . mysql_error());
echo "Success!<br/>";
// ------------------------------
// Close connection
// ------------------------------
mysql_close($con);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment