如何为我的网站应用程序编写MySQL DB单连接字符串
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.
我有一个使用HTML,JS,PHP构建的网站。 我使用Linux OS和apache2服务器以及MySQL作为后端。 要连接到MySQL服务器,我已经在每个PHP页面中创建了连接,但是我希望将这个连接字符串写在一个文件中(就像Visual Studio中的web.config一样),这样我就可以轻松地更改连接字符串。 我已经安装了PHPMYADMIN,我可以在该文件夹中看到几个配置文件。但是我不知道在哪里需要编写这个连接字符串? p>
或者我是否应该使用连接字符串创建一个PHP文件并在每个其他PHP文件中包含()此文件并创建连接实例? p> 我没有使用任何IDE来构建网站。 p>
非常感谢任何帮助。 p>
非常感谢。 p> \ n div>
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.