Cannot connect to database but I am doing everything right - Help

Hi, I have this message on the browser: Cannot connect to database id1776712_pms
However I checked all the codes and settings, and can not find anything wrong.
My php connection code:
function webConnect()
{ // Declare connection variables
$hostname = ‘localhost’;
$username = ‘id1776712_healthinforsys’;
$password = ‘xxx’;
$dbname = ‘id1776712_pms’;
$connection = @mysqli_connect($hostname, $username, $password) or die("Cannot connect to " . $hostname . " with username " . $username);
@mysqli_select_db($dbname, $connection) or die("Cannot connect to database " . $dbname);
return $connection;
}
$query = ‘select * from country’;
$result = mysqli_query(webConnect(), $query) or die('Query error: ’ . mysqli_error());

The above username and dbname are correct - checked on phpMyAdmin.

Now it is showing this message on the browser:
Cannot connect to localhost with username id1776712_healthinforsys

This is the code you are looking for.

function webConnect()
{ 

$hostname = 'localhost';
$username = 'id1776712_healthinforsys';
$password = 'xxx';
$dbname = 'id1776712_pms';

$connection = mysqli_connect($hostname, $username, $password) or die("Cannot connect to " . $hostname . " with username " . $username);

mysqli_select_db($connection, $dbname) or die("Cannot connect to database " . $dbname);

return $connection;
}

$query = 'SELECT * FROM country';
$result = mysqli_query(webConnect(), $query) or die('Query error: ' . mysqli_connect_error());

It worked. Thanks a lot. :-))

1 Like