in Education by
I make a website on php and want to read some information from text file. the reason is if, i want to update connection string, data base name, hyperlinks,echo messages etc. then i don't want to update my php code i just replace that text in text file and the relative changes reflects in php file. For example, 1. A php file : <?php $servername = "Server_Name"; //Comes from db_config.txt file $username = "User_Name"; //Comes from db_config.txt file $password = "Password"; //Comes from db_config.txt file // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Conn_Succ_Msg"; //Comes from db_config.txt file ?> 2. A text file like : (db_config.txt) Server_Name = localhost User_Name = abc Password = 123 Conn_Succ_Msg = Connected successfully I study "PHP 5 File Open/Read/Close" from http://www.w3schools.com/php/php_file_open.asp but not getting how to implement this even if it possible in xml file, etc also fine. Or can it possible with java scripts, jquery etc. Any idea how to do this ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
try it : <?php $file_handle = fopen("db_config.txt", "r"); $ser = array(); $i = 0; while (!feof($file_handle)) { $ser[$i] = fgets($file_handle); $i++; } fclose($file_handle); $servername = $ser[0]; $servername = explode("=", $servername); $servername = trim($servername[1]); $username = $ser[1]; $username = explode("=", $username); $username = trim($username[1]); $password = $ser[2]; $password = explode("=", $password); $password = trim($password[1]); $Conn_Succ_Msg = $ser[3]; $Conn_Succ_Msg = explode("=", $Conn_Succ_Msg); $Conn_Succ_Msg = trim($Conn_Succ_Msg[1]); $servername = $servername; $username = $username; $password = $password; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo $Conn_Succ_Msg; ?> open file and get line and expload line with = and trim for remove spaces . also you can using expload and trim in while : $file_handle = fopen("db_config.txt", "r"); $ser = array(); $i = 0; while (!feof($file_handle)) { $ser[$i] = fgets($file_handle); $sers = explode("=", $ser[$i]); $ser[$i] = trim($sers[1]); $i++; } fclose($file_handle); $servername = $ser[0]; $username = $ser[1]; $password = $ser[2]; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo $ser[3];

Related questions

0 votes
    I make a website on php and want to read some information from text file. the reason is if, i ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    read the data from employee. text file and sort on age using counting sort,merge sort,quick sort and write ... sortedemponage.txt'. Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    Write a program which sends the name of a text file from the client to server and display the message on the ... file present or not. Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    1. Write a function in python to read the content from a text file “poem.txt” line by line and display the same on screen Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    So this is a zany thing that I'm attempting to do, but what I am trying to achieve is querying ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    So this is a zany thing that I'm attempting to do, but what I am trying to achieve is querying ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Okay, so let's say I have a webpage with a defined php $variable, and then I use include(" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Okay, so let's say I have a webpage with a defined php $variable, and then I use include(" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Okay, so let's say I have a webpage with a defined php $variable, and then I use include(" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    So I tried reading the file and I confirmed it worked and then tried adding the contents to a listbox ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Need to show a credits screen where I want to acknowledge the many contributors to my application. Want it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    A Web site that allows users to enter text, such as a comment or a name, and then stores ... Application Security in chapter Database Programming Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
...