Home > Backend Development > PHP Tutorial > PHP access database configuration common method (json)

PHP access database configuration common method (json)

不言
Release: 2023-03-29 06:02:02
Original
1626 people have browsed it

This article mainly introduces the general method (json) for PHP to access database configuration. It has certain reference value. Now I share it with you. Friends in need can refer to it.

The purpose is to access through general classes The configuration file method provides dynamic acquisition and setting of database connections, so that it can provide flexible, simplified, and decoupled operation methods during development and production applications. Friends in need can refer to the following

Extraction A common method for database configuration

The purpose is to provide dynamic acquisition and setting of database connections by accessing configuration files through common classes, so that flexible and simplified configurations can be provided during development and production applications. , decoupled operation mode. For example, two sets of database access content are configured in the configuration file, one is the test library access address, and the other is the production library access. When you need to obtain the connection information, you only need to fill in the matching parameters

Structure

  • Factory mode, pass parameters, return results;

  • Returns array successfully, otherwise returns string error message;

  • Two files work together, one is a json format configuration file, which is responsible for saving the database connection content, and the other is a reading class, which is responsible for reading and retrieval, and returning results;

Usage

  • Define the configuration file as db-config.json, the content is database connection information;

  • Reading classdb-config.php Provides public static access for upper-layer interface calls;

  • Upper-layer interface uses the interface Method $db_conf = DbConf::Conf("debug");Retrieve database connection information with the debug flag. If $db_conf is in array format;

see code

{
 "debug": {
  "db_host": "",
  "db_name": "",
  "db_user": "",
  "db_password": ""
 }
}
Copy after login

php class

<?php
class DbConf{
 public static function Conf( $conf_name ){
  
  if(empty($conf_name)){
   die("Illegal parameter");
  }

  $from = "localhost"; // allow legal host only
  if(!isset($_SERVER[&#39;HTTP_HOST&#39;]) || $_SERVER[&#39;HTTP_HOST&#39;]!=$from){
   die("Unauthorized access");
  }

  $json_config = file_get_contents(&#39;db-config.json&#39;);

  $json_data = json_decode($json_config, true);

  if( array_key_exists($conf_name, $json_data)){
   return $json_data[$conf_name];
  }else{
   return "Not Found";
  }
 }
}
Copy after login

That’s it for this article. Friends in need can refer to it

Related recommendations:

PHP connects to the MySQL database and outputs it in json format

The above is the detailed content of PHP access database configuration common method (json). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template