Summarizing _php instances thinkphp connecting to a database

Source: Internet
Author: User
Tags dsn

The examples in this article summarize several common ways to connect a thinkphp database. Share to everyone for your reference. Specifically as follows:

thinkphp built-in abstract database access layer, to encapsulate different database operations, we only need to use the common DB class to operate, without having to write different code and the underlying implementation for different databases, the DB class will automatically invoke the corresponding database adapter to handle, the current database includes MySQL, MsSQL, Pgsql, Sqlite, Oracle, IBase, and PDO support, if the application needs to use a database, you must configure the database connection information, the database configuration file can be defined in several ways:

First: Define in the project configuration file

Copy Code code as follows:
Return Array (
' Db_type ' => ' MySQL ',
' Db_host ' => ' localhost ',
' Db_name ' => ' thinkphp ',
' Db_user ' => ' root ',
' Db_pwd ' => ',
' Db_port ' => ' 3306 ',
' Db_prefix ' => ' think_ ',
Other project configuration parameters .....
);

This approach is recommended because the database access configuration for a general project is the same, and the method system is automatically acquired when connecting to the database without manual connection.

You can define different database connection information for each project, and you can define configuration information for the debug database in the Debug configuration file (conf/debug.php), and if the database connection information is defined in the project configuration file and debug mode configuration file, the latter takes effect under debug mode. The former takes effect under Deployment mode.

The second way to use DSN to pass parameters when initializing a DB class

Copy Code code as follows:
$db _dsn = "Mysql://username:passwd@localhost:3306/dbname";
$db = new db ($db _DSN);

This approach is primarily used to manually connect to a database within the controller, or to create multiple database connections.

The third way to use an array to pass parameters

Copy Code code as follows:
$DSN = Array (
' DBMS ' => ' MySQL ',
' username ' => ' username ',
' Password ' => ' password ',
' hostname ' => ' localhost ',
' Hostport ' => ' 3306 ',
' Database ' => ' dbname '
);
$db = new db ($DSN);

This is also the case for manually connecting to a database, or for creating multiple database connections.

The fourth type is defined inside the model class

Copy Code code as follows:
Protected $connection = Array (
' DBMS ' => ' MySQL ',
' username ' => ' username ',
' Password ' => ' password ',
' hostname ' => ' localhost ',
' Hostport ' => ' 3306 ',
' Database ' => ' dbname '
);
Or use the following definition
protected $connection = "Mysql://username:passwd@localhost:3306/dbname";

If the connection attribute is defined within a model class, the database connection information is used for database connections when the model object is instantiated, and is typically used for databases that are located outside the current database connection.

Thinkphp does not connect to the database at the outset, but only when there is a data query operation, the additional situation is that when the system first operates the model, the framework automatically connects the database to get the data field information of the relevant model class and caches it.

(Field cache directory: Runtime/data/_fields)

thinkphp support PDO, if you want to connect to the database using the PDO method, you can refer to the following settings.

We use the Project profile definition as an example to illustrate:

Copy Code code as follows:
Return Array (
' Db_type ' => ' PDO ',
Note that the configuration of the DSN differs for different databases please refer to the PHP Manual PDO class Library section
' Db_dsn ' => ' Mysql:host=localhost;dbname=think ',
' Db_user ' => ' root ',
' Db_pwd ' => ',
' Db_prefix ' => ' think_ ',
Other project configuration parameters .....
);

When using the PDO method, be careful to check whether the associated PDO module is open, and the DB_DSN parameter is only valid for PDO connection.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course" and "thinkphp Common methods Summary"

I hope this article will help you with the PHP program design based on 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.