Home > Article > Backend Development > How to design database in php
The first step is generally to create a database, unless you are using a third-party database service.
When a database is created, an owner is assigned to execute and create new statements.
Normally, Only the owner (or super user) has the authority to perform any operations on objects in the database.
If you want other users to use it, you must give them permission. (Recommended learning: PHP Programming from Beginner to Master)
Applications should never use the database owner or superuser account to connect to the database, because these accounts can execute Any operation, such as modifying the database structure (such as deleting a table) or clearing the entire database.
Separate database accounts should be created for each aspect of the program and given very limited permissions on database objects.
Assign only the permissions required to complete its functions to prevent the same user from completing the tasks of another user. In this way, even if an attacker uses a program vulnerability to gain access to the database, he or she can only have the same scope of impact as the program.
Encourage users not to implement all transaction logic in web applications (i.e. user scripts).
It is best to use views, triggers or rules to complete it at the database level. When the system is upgraded, new interfaces need to be opened for the database, and all database clients must be redone.
In addition to this, triggers can handle fields transparently and automatically, and provide useful information when debugging programs and tracing facts.
For example: Use singleton mode (Singleton) to create a database connection class
Database connection objects are usually shared throughout the project, there is no need to Instantiating an object every time it is used is not only inefficient but also wastes resources. Therefore, a singleton class is used to ensure that it is unique in the entire application system.
Singleton mode classes are usually implemented using the static class method getInstance(). This static method only returns a unique instance of the class. The first time this method is called, the method creates an instance, stores it in a private static variable, and returns the instance. On the next call, no new instance will be created, but the instance created the first time will be returned.
The constructor of a class using singleton mode is usually set to private to prevent the class from being directly instantiated and creating a new instance.
<?php Class DBConnect { private static $Instance = null; public static function getInstance() { if( !isset(self::$Instance) ) { self::$Instance = new self(); } return self::$Instance; } private function __construct() { … } private function __clone() {} } ?>
The above is the detailed content of How to design database in php. For more information, please follow other related articles on the PHP Chinese website!