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 mysqli object in php, connect to the database, and set its encoding. Later, I found that the application process was particularly troublesome. every time I used include to import these files, and then I used $ db to access the database. However, I processed them in some classes, create a database connection in the constructor to close the connection in the destructor, and then use the connection in each function .... Class database access php create close database Time database connection mysql
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?
The writing is messy. please help me.
Thank you!