1

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.

2
  • 3
    The usual way is the latter, make a separate PHP file with the config and include that where needed. Commented Jul 29, 2013 at 8:51
  • Thanks a lot folks for your valuable suggestions. Commented Jul 29, 2013 at 9:14

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

2

Create a PHP file with connection setting and just include it in all pages at the top.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.