Answer the question
In order to leave comments, you need to log in
Why is there an error connecting to the database?
What a nonsense. If I specify the root login directly, then it connects. And if I pass it through a variable, I get the following error Warning: mysqli_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in C:\openserver\OSPanel\domains\darkdate\ www\modules\config.php on line 9
who can tell me what's wrong?
this is how it works
and this is how it throws an error
Answer the question
In order to leave comments, you need to log in
Your variables are declared outside of the function.
There are three solutions:
1. The function must accept arguments, and when called, you will pass them ( recommended ):
function connect($server, $login, $password, $db) {
return mysqli_connect($server, $login, $password, $db)
}
$connection = connect($server, $login, $password, $db);
function connect() {
global $server;
global $login;
global $password;
global $db;
return mysqli_connect($server, $login, $password, $db);
}
$connection = connect($server, $login, $password, $db)
$connect = function() use ($server, $login, $password, $db) {
return mysqli_connect($server, $login, $password, $db);
};
$connection = $connect();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question