db无法在php中连接

时间:2014-10-17 08:13:57

标签: php mysql database-connection

我有一个脚本,我需要连接MySQL db。但不知何故连接失败了。任何人都可以帮我检查问题吗?谢谢。

$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';
}

我运行查询的那一刻,我收到了这个错误:

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

我已将用户名设置为' root',但似乎无法收到用户名。顺便说一句,第一次连接成功。就是这样,当它连接到数据库的那一刻,就出现了错误。

1 个答案:

答案 0 :(得分:0)

您需要授予用户授权权限" root"如果您想要外部访问数据库vti_ctes_demo。

Use this to grant permission to the user