Soon after I learned php, I was writing a graduation project using php, but I made a mistake in database connection. I first used the configuration file method in config. inc. some database connection parameters are set in php, such as database name and password. conn. create a m... in php... soon after I learned php, I was writing a graduation project using php, but it was difficult to connect to the database,
The configuration file method is used at the beginning,
Some database connection parameters are set in config. inc. php, such as database name and password,
Create a mysqli object in db. conn. php, connect to the database, and set its encoding.
Later, I found that the application process was particularly troublesome. Each time I used include to import these files, and then I used
$ Db is used to access the database, but it is later processed in some classes and intends to create a database connection in the constructor
Close the connection in the Destructor and use the connection in each function.
So what should we do at this time? Is it necessary to create a variable in each class to save the database connection?
Then assign an opened database connection to the constructor and disable it in the destructor?
A mess of writing... Please enlighten me
Reply content:
Soon after I learned php, I was writing a graduation project using php, but it was difficult to connect to the database,
The configuration file method is used at the beginning,
Some database connection parameters are set in config. inc. php, such as database name and password,
Create a mysqli object in db. conn. php, connect to the database, and set its encoding.
Later, I found that the application process was particularly troublesome. Each time I used include to import these files, and then I used
$ Db is used to access the database, but it is later processed in some classes and intends to create a database connection in the constructor
Close the connection in the Destructor and use the connection in each function.
So what should we do at this time? Is it necessary to create a variable in each class to save the database connection?
Then assign an opened database connection to the constructor and disable it in the destructor?
A mess of writing... Please enlighten me
The reason is that you didn't use object-oriented to solve the problem.
Write a superclass to take the database link and place the link in $ this-> db. Then, other business classes inherit this class. Could you tell me, do you still need to create a variable for each class to save the database connection? Is it because $ this-> db gets the database connection.
I am only thinking about this, but I should not do this in design. Data operations should be placed in the Model rather than directly in the Controller.