db无法在php中连接

I have a script where I need to connect to MySQL db. But somehow the connection is failed. Can anyone help me to check the problem? Thanks ahead.

$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "vti_ctes_demo";

$con = mysql_connect($hostname, $username, $password);
if (!$con) {
    die('Could not connect: ' . mysql_error());
}else
{
    echo 'connected';   
}

// make foo the current db
$db_selected = mysql_select_db($dbname, $con);
if (!$db_selected) {
    die ('Can\'t use vti_ctes_demo : ' . mysql_error());
}else
{
    echo 'connected';
}

The moment I run the query, I get this error:

Can't use vti_ctes_demo : Access denied for user ''@'localhost' to database 'vti_ctes_demo'

I have set the username as 'root', but it seems like it can't receive the username. Btw, first connection is successful. Just that, the moment when it's connected to the db, then the error appeared.

You need grant privileges to the user "root" if you want external access to database vti_ctes_demo.

Use this to grant permission to the user