Several ways to connect ThinkPHP to the database

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

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

  1. Return array (
  2. 'Db _ type' => 'mysql ',
  3. 'Db _ host' => 'localhost ',
  4. 'Db _ name' => 'thinkphp ',
  5. 'Db _ user' => 'root ',
  6. 'Db _ pwd' => '',
  7. 'Db _ port' => '123 ',
  8. 'Db _ prefix' => 'think _',
  9. // Other project configuration parameters .........
  10. );

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.

  1. $ Db_dsn = "mysql: // username: passwd @ localhost: 3306/DbName ";
  2. $ 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

  1. $ DSN = array (
  2. 'Dbms '=> 'mysql ',
  3. 'Username' => 'username ',
  4. 'Password' => 'password ',
  5. 'Hostname' => 'localhost ',
  6. 'Hostport' => '123 ',
  7. 'Database' => 'dbname'
  8. );
  9. $ 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.

  1. Protected $ connection = array (
  2. 'Dbms '=> 'mysql ',
  3. 'Username' => 'username ',
  4. 'Password' => 'password ',
  5. 'Hostname' => 'localhost ',
  6. 'Hostport' => '123 ',
  7. 'Database' => 'dbname'
  8. );
  9. // Or use the following definition
  10. 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:

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

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.

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.