connecting to a database
thinkphp built-in abstract database access layer, to encapsulate different database operations, we only need to use the common DB classes to operate without the need for different databases to write different code and the underlying implementation, the DB class will automatically invoke the corresponding database driver to handle. Current databases include MySQL, SQL Server, Pgsql, Sqlite, Oracle, IBase, Mongo, and include support for PDO.
If the application needs to use a database, you must configure the database connection information, and the database configuration file can be defined in several ways.
One, global configuration definition
The common configuration is to add the following configuration parameters to the application configuration file or to the module configuration file:
Database configuration information ' Db_type ' => ' mysql ',//database type
' db_host ' => ' localhost ',//server address
' db_name ' => ' thinkphp ',//database name
' db_user ' => ' root ',//username
' db_pwd ' => ' 123456 ',//password
' Db_port ' => 3306,//Port
' db_prefix ' => ' think_ ',//database table prefix ' db_charset ' => ' UTF8 '
,//Character set
database type by db_ Type parameter setting.
The following are the database settings that are currently supported:
Connect the database __ Database >
If Db_type uses the PDO type, the database type is determined by the DB_DSN configuration.
Or use the following configuration
' Db_dsn ' => ' Mysql://root:123456@localhost:3306/thinkphp#utf8 '
Use the Db_dsn method definition to simplify configuration parameters, and the DSN parameter format is:
Database type://Username: password @ database address: Database port/Database name # Character Set
Character set settings are valid for more than 3.2.1 versions, and the character set, if not set, defaults to UTF8.
If both configuration parameters are present, the DB_DSN configuration parameter takes precedence.
Note: If you are setting up a distributed database, DB_DSN configuration is not currently supported.
If you are using the PDO driver, you must first configure **db_type * to PDO, and then you need to configure additional parameters separately, for example:
PDO connection mode
' Db_type ' => ' PDO ',//database type
' Db_user ' => ' root ',//username
' db_pwd ' => ',// Password
' db_prefix ' => ' think_ ',//database table prefix
' db_dsn ' => ' mysql:host=localhost;dbname=thinkphp;charset= UTF-8 '
Note: The PDO way of DB_DSN configuration format is different, depending on the database type settings vary, you can refer to the PHP manual.
The database connection information defined by the configuration file is typically used by the system by default, because the database access configuration for a general application is the same. The method system is automatically acquired when connecting to the database without manual connection.
You can define different database connection information for each module, and if debug mode is turned on, you can also define separate database configuration information in different application state configuration files.
Ii. model class definition
If the connection attribute is defined in a model class, the custom model is instantiated with the defined database connection information, rather than the default connection information set in the configuration file, which is typically used for some data tables other than the current database connection, for example:
Set up the database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
protected $connection = Array (
' db_type ' => ' mysql ',
' db_user ') => ' root ',
' db_pwd ' => ' 1234 ', ' db_host ' => ' localhost ' ,
' db_port ' => ' 3306 ',
' db_name ' => ' thinkphp ', ' db_charset ' => ' UTF8 ' ,
);
You can also define the DSN method, for example:
Set up the database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
//or using DSN definition
protected $connection = ' mysql://root:1234@localhost:3306/ Thinkphp#utf8 ';
}
If we have configured additional database connection information in the configuration file, for example:
Database configuration 1
' DB_CONFIG1 ' => array (
' db_type ' => ' mysql ',
' db_user ' => ' root ',
' db_ PWD ' => ' 1234 ',
' db_host ' => ' localhost ' ,
' db_port
' => ' 3306 ', ' db_name ' => ' thinkphp ',
' db_charset ' => ' UTF8 ',
),
//Database Configuration 2
' db_config2 ' => ' MySQL ://root:1234@localhost:3306/thinkphp#utf8 ';
So, we can change the attribute definition of the model class to:
Set up the database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
//Calling configuration file database Configuration 1
protected $connection = ' db_config1 ';
}
Set up the database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Infomodel extends model{
//Calling configuration file database Configuration 1
protected $connection = ' Db_config2 ';
}
Third, the definition of instantiation
In addition to specifying database connection information when the model is defined, we can also specify database connection information when instantiated, for example, if the M method instantiation model is used, it can also support incoming different database connection information, for example:
Represents the instantiated user model, which is connected to the Other_user table of the demo database, with the connection information being configured with the third parameter. If we have already configured DB_CONFIG2 in the project configuration file, we can also use:
$User = M (' User ', ' other_ ', ' db_config2 ');
It should be noted that the thinkphp database connection is inert, so it is not connected to the database at the time of instantiation, but is connected to the database when there is actual data manipulation (in addition, when the system first instantiates the model, Automatically connects to the database to get field information for the data table corresponding to the related model class.
Note: This article comes from the documentation manuals consulted in the development.