I have a txt file which contains the log in credentials separated by newline. I want to pick up data from that file and set the connection string according to that. Here's the code for that.
$db = array(3);
$myFile = "sqlaccess.txt";
$handle = fopen($myFile, 'r');
if(!feof($handle))
{
for($i=0;$i<4;$i++)
{
$db[$i] = fgets($handle);
echo $db[$i]; echo "<br>";
}
}
else
{
fclose($handle);
}
$dbhost = $db[0];
$dbuser = $db[1];
$dbpass = $db[2];
$dbname = $db[3];
the echo command displays everything correctly as saved in the file. Now the connection string is:
$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
This is not working. Connection fails
but connection is succesful if i hard code this as follows:
$conn = mysqli_connect('localhost','root','password', 'Newdb');
but hardcoding is not a good practice. so What could be going wrong in my code??
$db[$i] = trim(fgets($handle));file()which will return a file as an array, instead of all that extra fopen/for code.