简体   繁体   中英

Warning: mysqli::mysqli(): (HY000/1044): Access denied for user

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');

errors:

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) 

I have created my_db in localhost:82/phpmyadmin ...

I also have created a user=username ,with password=password ...

Also if I change the if (!defined('$servername')) define('$servername', 'localhost'); to define('$servername','localhost'); I get:

already defined variables on the top of my site in addition to undefined server and db

you were defining them the wrong way, try this sample :

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM