为什么获胜';t我的PHP文件连接到mysql


Why won't my PHP file connect to mysql?

我使用的是OS X El Capitan。Mysql正在运行,我可以使用Mysql命令行工具添加表。当我试图从我的PHP文件连接到数据库时,我得到了一个1045错误。以下是代码:

$dbc = mysqli_connect('127.0.0.1', 'root', '*****', 'aliendatabase')
// or die('Error connecting to MySQL server. ');
or die("Debugging errno: " . mysqli_connect_errno() );
$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
mysqli_close($dbc);

我已将拨款选项设置如下:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*477A2FE5488EA451D53F4BFE898AA4D4F61B68EF' WITH GRANT OPTION GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION

我不知道为什么这不起作用,我已经尝试用mysql.server startmysqld启动mysql进程。显然,连接参数中的星号代表我的密码。有人知道我缺了什么吗?

您正在授予localhost的权限,然后连接到127.0.0.1

尝试在连接中将127.0.0.1更改为localhost

$dbc = mysqli_connect('localhost', 'root', '*****', 'aliendatabase')

或者至少确保root@127.0.0.1也被授予了适当的权限。