警告:mysqli :: mysqli():( HY000 / 1044):拒绝用户访问

时间:2015-08-28 19:20:01

标签: php mysql mysqli

if (!defined('$servername')) define('$servername', 'localhost');
if (!defined('$username')) define('$username', 'username');
if (!defined('$password')) define('$password', 'password');
if (!defined('$dbname')) define('$dbname', 'my_db');

$conn = new mysqli($servername, $username, $password, 'my_db');

错误:

Notice: Undefined variable: servername in C:\Program Files (x86)\XAMPP\htdocs\amit\Databas.php on line 10

Warning: mysqli::mysqli(): (HY000/1044): Access denied for user ''@'localhost' to database 'my_db' in C:\Program Files (x86)\XAMPP\htdocs\amit\Databas.php on line 10
Connection failed: Access denied for user ''@'localhost' to database 'my_db'
__________________________________________________
$conn = new mysqli($servername, $username, $password, $dbname);
Notice: Undefined variable: dbname in C:\Program Files (x86)\XAMPP\htdocs\amit\Databas.php on line 10 /
on the top of the page
__________________________________________________
$conn = new mysqli('localhost', 'username', 'password', 'my_db');
errors:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\Program Files (x86)\XAMPP\htdocs\amit\Databas.php on line 10
Connection failed: Access denied for user 'username'@'localhost' (using password: YES) 

我在my_db ...

中创建了localhost:82/phpmyadmin

我还创建了一个user=usernamepassword=password ...

此外,如果我将if (!defined('$servername')) define('$servername', 'localhost');更改为define('$servername','localhost');,我会:

除了未定义的服务器和db

之外,

已在我的站点顶部定义了变量

1 个答案:

答案 0 :(得分:2)

你用错误的方式定义它们,试试这个样本:

if (!defined('SERVERNAME')) define('SERVERNAME', 'localhost');
    if (!defined('USERNAME')) define('USERNAME', 'username');
    if (!defined('PASSWORD')) define('PASSWORD', 'password');
    if (!defined('DBNAME')) define('DBNAME', 'my_db');

    $conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, 'my_db');