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=username
,password=password
...
此外,如果我将if (!defined('$servername')) define('$servername', 'localhost');
更改为define('$servername','localhost');
,我会:
已在我的站点顶部定义了变量
答案 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');