I will raise two options.
There are several ways to get the contents of a .txt file.
A couple of examples:
$data=explode("\n", file_get_contents('data.txt'));
or
$data=file('data.txt', FILE_IGNORE_NEW_LINES);
Option 1
The file only contains the values of the connection.
File .txt
127.0.0.1
root
root
dbname
The variable $data
would have an output ( var_dump ) similar to this:
array (size=4)
0 => string '127.0.0.1' (length=9)
1 => string 'root' (length=4)
2 => string 'root' (length=4)
3 => string 'dbname' (length=6)
We would only have to assign each element of the array to each variable.
It would be something like this:
$data=explode("\n", file_get_contents('data.txt'));
$host=$data[0];
$user=$data[1];
$pass=$data[2];
$db =$data[3];
$conn=new mysqli($host, $user, $pass, $db);
Option 2
The file contains both the values of the connection and the name of the variables.
File .txt
host=127.0.0.1
user=root
pass=root
db=quiniela_test
The variable $data
would have an output ( var_dump ) similar to this:
array (size=4)
0 => string 'host=127.0.0.1' (length=14)
1 => string 'user=root' (length=9)
2 => string 'pass=root' (length=9)
3 => string 'db=dbname' (length=9)
You just have to create a loop to sign the names of the variables automatically and their corresponding value. It would be something like this:
$data=explode("\n", file_get_contents('data.txt'));
foreach($data as $value) {
$item =explode('=', $value);
${$item[0]}=$item[1];
}
$conn=new mysqli($host, $user, $pass, $db);
If I did not make a mistake in writing it, the two options should work.