Database access in class-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 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!

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.