It is a good time to create and close database connections in php development.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.