访问被拒绝使用MySQL WorkBench生成的PHP连接到数据库

I've seen a couple of similar posts, but none really help with my problem. I'm trying to connect to a database I created by using MySQL workbench. Under my user privileges in Workbench there's a GUI which allows you to enable all grants and permissions.

When I run the script show grants for phpTester@localhost; it shows:

GRANT ALL PRIVILEGES ON *.* TO 'phpTester'@'localhost' WITH GRANT OPTION

So I know I have all permissions, and WorkBench also has a plug-in where you can copy your SQL to a clipboard and it will automatically extract your database settings so you can just copy and paste into your PHP script. So my script looks like this:

define('DB_NAME', 'forms1');
define('DB_USER', 'phpTester');
define('DB_PASSWORD', '12345');
define('DB_HOST', 'localhost');
define('DB_PORT', 2080);


$link = mysql_connect(DB_HOST, DB_USER,DB_PASSWORD, DB_PORT);

if (!$link){
  die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
  die('Not Working ' . DB_NAME . ':' . mysql_error());  
} 

It fails with the error:

Warning: mysql_connect(): Access denied for user 'phpTester'@'localhost' (using password: YES) on line 10
Could not connect: Access denied for user 'phpTester'@'localhost' (using password: YES)

And When I use this line of PHP:

$link = mysql_connect(DB_HOST, DB_USER);

I get this error:

Not Working forms1:Access denied for user ''@'localhost' to database 'forms1'.

But I do have a password set for this user, and I am using xampp for my localhost.

查看全部
drhs13583567608
drhs13583567608
2015/02/18 04:12
  • php
  • mysql
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复