PHP access database configuration general method (json) clever

jacklove
Release: 2023-04-01 18:22:01
Original
1302 people have browsed it

The purpose is to provide dynamic acquisition and setting of database connections through universal class access to configuration files, so that flexible, simplified, and decoupled operations can be provided during development and production applications. Friends who need it can Refer to the following

Extract a common method for database configuration

The purpose is to provide dynamic acquisition and setting of database connections through common classes to access configuration files, so that development time It can provide flexible, simplified, and decoupled operation methods for production applications. 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, responsible for saving the database connection content, and the other is a reading class, responsible for reading and retrieval, and returning results;

Usage

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

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

  • Upper layer The interface uses the interface method$db_conf = DbConf::Conf("debug");Retrieve the 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


        
Copy after login

This article is introduced here, friends in need can refer to it

You may be interested Article:

Related explanations on how PHP connects to the MySQL database and outputs it in json format

PHP automatically identifies the current mobile terminal. Related explanations

PhpStorm local breakpoint debugging method

The above is the detailed content of PHP access database configuration general method (json) clever. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!