How ThinkPHP connects to the database

Source: Internet
Author: User
Tags dsn
This article mainly introduces how ThinkPHP connects to the database, including the definition of the project configuration file, parameter passing through the DSN method, parameter passing through the array, and definition in the model class, which is of great practical value, for more information about how ThinkPHP connects to the database, see the examples in this article. Share it with you for your reference. The details are as follows:

ThinkPHP has a built-in abstract database access layer to encapsulate different database operations. we only need to use public Db classes for operations, instead of writing different code and underlying implementation for different databases, the Db class automatically calls the corresponding database adapter for processing, the current database includes support for Mysql, MsSQL, PgSQL, Sqlite, Oracle, Ibase, and PDO. if the application needs to use the database, you must configure the database connection information, database configuration files can be defined in multiple ways:

First: defined in the project configuration file

The code is as follows:

Return array (
'Db _ type' => 'mysql ',
'Db _ host' => 'localhost ',
'Db _ name' => 'thinkphp ',
'Db _ user' => 'root ',
'Db _ pwd' => '',
'Db _ port' => '123 ',
'Db _ prefix' => 'think _',
// Other project configuration parameters .........
);


This method is recommended because the database access configuration of a project is the same. The system automatically obtains this method when connecting to the database, without manual connection.

You can define different database connection information for each project, and you can also debug the configuration file (Conf/debug. php), which defines the configuration information of the debugging Database. if the database connection information is defined in both the project configuration file and the debugging mode configuration file, the latter takes effect in the debugging mode, the former takes effect in the deployment mode.

Second, use the DSN method to pass parameters when initializing the Db class.

The code is as follows:

$ Db_dsn = "mysql: // username: passwd @ localhost: 3306/DbName ";
$ Db = new Db ($ db_dsn );


This method is mainly used to manually connect to the database in the controller, or to create multiple database connections.

Third, use array to pass parameters

The code is as follows:

$ DSN = array (
'Dbms '=> 'mysql ',
'Username' => 'username ',
'Password' => 'password ',
'Hostname' => 'localhost ',
'Hostport' => '123 ',
'Database' => 'dbname'
);
$ Db = new Db ($ DSN );


This method is also used to manually connect to the database, or to create multiple database connections.

The fourth type is defined in the model class.

The code is as follows:

Protected $ connection = array (
'Dbms '=> 'mysql ',
'Username' => 'username ',
'Password' => 'password ',
'Hostname' => 'localhost ',
'Hostport' => '123 ',
'Database' => 'dbname'
);
// Or use the following definition
Protected $ connection = "mysql: // username: passwd @ localhost: 3306/DbName ";


If the connection attribute is defined in a model class, the database connection information will be used when the model object is instantiated, data tables are usually used in other databases except the current database connection.

ThinkPHP does not connect to the database at the beginning, but connects to the database only when data query is performed. In addition, when the system first operates the model, the framework automatically connects to the database to obtain the data field information of the relevant model class and caches it.

(Field cache Directory: Runtime/Data/_ fields)

ThinkPHP supports the PDO method. to connect to the database using the PDO method, refer to the following settings.

Take the project configuration file definition as an example:

The code is as follows:

Return array (
'Db _ type' => 'pdo ',
// Note that the DSN configuration varies with different databases. see the PDO class library section in the PHP Manual.
'Db _ DSN '=> 'MySQL: host = localhost; dbname = think ',
'Db _ user' => 'root ',
'Db _ pwd' => '',
'Db _ prefix' => 'think _',
// Other project configuration parameters .........
);


When using the PDO mode, check whether the related PDO module is enabled. the DB_DSN parameter is valid only for the PDO mode connection.

I hope this article will help you design PHP programs based on the ThinkPHP framework.

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.