How to pass variables in the MySQL connection string in php -


i have txt file contains log in credentials separated newline. want pick data file , set connection string according that. here's code 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 correctly saved in file. connection string is:

$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);   

this not working. connection fails

but connection succesful if hard code follows:

$conn = mysqli_connect('localhost','root','password', 'newdb'); 

but hardcoding not practice. going wrong in code??

fgets not strip \n returned string, either need trim them yourself:

$db[$i] = trim(fgets($handle)); 

or use file function replace read loop:

$db = file('sqlaccess.txt'); 

if choose latter, code simplified to:

$myfile = 'sqlaccess.txt'; $db = file($myfile);  $dbhost = $db[0]; $dbuser = $db[1]; $dbpass = $db[2]; $dbname = $db[3]; 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -