Saturday 27 June 2015

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);
?>

0 comments:

Post a Comment