I have a website built using HTML,JS,PHP. I use Linux OS and apache2 server and MySQL as a back end. To connect to the MySQL server I have created connection in each PHP page,But I want this connection string to be written in a single file(Like web.config in Visual studio),so that I can change the connection string easily whenever it is necessary.I have installed PHPMYADMIN and I can see couple of config files in that folder.But I do not know where I need to write this connection string?
Or shall I create a PHP file with the connection string and include() this file in every other PHP files and create the instance of the connection?
I am not using any IDE for building the website.
Any help is greatly appreciated.
Thanks a lot.
Create a PHP file where you connect to the SQL server and save the connection in a variable. Include this file everywhere you need the connection.
Pseudo example
init.php
$db_connection = connect_to_db($server, $username, $password, $db);
other_files.php
include "init.php";
execute_query("query", $db_connection);
Create a PHP file with connection setting and just include it in all pages at the top.
http://php.net/manual/en/pdo.connections.php Here is an exampe with PDO Just put that in a php file and include it where is needed.