Last active
August 29, 2015 14:04
-
-
Save gayanvirajith/7b1aaafa180748a2934b to your computer and use it in GitHub Desktop.
Check MySQL db connection in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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