Detecting Database And MySQL Connections in PHP – PHP DBConnection

by Hiroshi on June 19, 2008

in MySQL, php mySQL

This example describes that how we can find that database and mySQL connections are established or not and what to do if one of the connections is not established. First we define dbuser, dbpassword and database name and then create a db connection and mysql connection and then check these if these are established and script can find the database and mysql. In case it can not locate any of these then we just tell the script to exit and stop proceeding.

$sDbUserName = "root";
$sDbPassword = "";
$sDatabase = "somedbname";
	$dbConnection = @mysql_connect("localhost", $sDbUserName, $sDbPassword);
	if (!$dbConnection)
		echo "Unable to connect to the database server at this time.";
		exit( );
	if (!@mysql_select_db($sDatabase, $dbConnection))
		echo "Unable to locate the database at this time.";
		exit( );

Related Posts

Previous post:

Next post: