tp5-database connection

Connect to the database

ThinkPHP has built-in abstract database access layer, encapsulating different database operations. We only need to use common DB classes for operations, instead of writing different codes and underlying implementations for different databases, Db classes will automatically call the corresponding database drivers to process. Using PDO mode, it currently includes the support of Mysql, SqlServer, PgSQL, Sqlite and other databases.
If an application needs to use a database, it must configure the database connection information. There are many ways to define the database configuration file.

Configuration file definition
The common configuration method is to add the following configuration parameters in database.php under the application directory or module directory:

return [
// Database type
'type' => 'mysql',
// Database Connection DSN Configuration
'dsn' => '',
// server address
'hostname' => '128.0.0.1',
// Database name
'database' => 'thinkphp',
// Database username
'username' => 'root',
// Database password
'password' => '',
// Database Connection Port
'hostport' => '',
// Database Connection Parameters
'params' => [],
// Database coding defaults to utf8
'charset' => 'utf8',
// Database table prefix
'prefix' => 'think_',
// Database Debugging Mode
'debug' => false,
// Database deployment: 0 centralized (single server), 1 distributed (master-slave server)
'deploy' => 0,
// Is it effective to separate master from slave in database reading and writing
'rw_separate' => false,
// Number of primary servers after read-write separation
'master_num' => 1,
// Specify slave server serial number
'slave_no' => '',
// Are fields strictly checked for existence?
'fields_strict' => true,
];

The type parameter supports the complete definition of the namespace. Without the namespace definition, the default namespace is think db connector. If you use your own extended database driver, you can configure it to:

// Database type
'type' => '\org\db\Mysql',

Connectors representing databases use the org db Mysql class as the database connection driver, rather than the default think db connector Mysql.
Each module can set independent database connection parameters, and the same configuration parameters can be set without repeated settings, for example, we can define in admin module's database.php configuration file:

return [
// server address
'hostname' => '192.168.1.100',
// Database name
'database' => 'admin',
];

The database address of the admin module is changed to 192.168.1.100, and the database name is changed to admin. The other connection parameters are the same as the configuration in the database.php of the application.
V5.0.6 + version began to support Mysql disconnection reconnection mechanism, default shutdown, if needed, is added to the database configuration file:

// Open disconnection and reconnect
'break_reconnect' => true,

Connection parameters

For different connections, database connection parameters can be added (specific connection parameters can be referred to the PHP manual). The built-in parameters include the following:

PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,

The connection configuration in the params parameter set in the database will be merged with the built-in setting parameter. If you need to use a long connection and return the lowercase column name of the database, you can define it in the following way:

'params' => [
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_CASE => \PDO::CASE_LOWER,
],

You can configure any PDO-supported connection parameters in params.

II. Method Configuration
We can dynamically define connection information when calling Db classes, such as:

Db::connect([
// Database type
'type' => 'mysql',
// Database Connection DSN Configuration
'dsn' => '',
// server address
'hostname' => '127.0.0.1',
// Database name
'database' => 'thinkphp',
// Database username
'username' => 'root',
// Database password
'password' => '',
// Database Connection Port
'hostport' => '',
// Database Connection Parameters
'params' => [],
// Database coding defaults to utf8
'charset' => 'utf8',
// Database table prefix
'prefix' => 'think_',
]);

Or use string mode:

Db::connect('mysql://root:1234@127.0.0.1:3306/thinkphp#utf8');

String connections are defined in the following format:
Database Type: //User Name: Password @Database Address: Database Port/Database Name #Character Set
Note: String mode may not be able to define certain parameters, such as prefix and connection parameters.
If we have already configured additional database connection information in the application configuration file (note that this is not a database configuration file), for example:

//Database Configuration 1
'db_config1' => [
// Database type
'type' => 'mysql',
// server address
'hostname' => '127.0.0.1',
// Database name
'database' => 'thinkphp',
// Database username
'username' => 'root',
// Database password
'password' => '',
// Database coding defaults to utf8
'charset' => 'utf8',
// Database table prefix
'prefix' => 'think_',
],
//Database Configuration 2
'db_config2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8';
//We can change it to
Db::connect('db_config1');
Db::connect('db_config2');

Definition of Model Class
If the connection attribute is defined in a model class, the model will automatically connect to a given database connection when it operates, rather than the default connection information set in the configuration file. It is usually used in other databases where some tables are located outside the current database connection, for example:

//Setting database connection information separately in the model
namespace app\index\model;

use think\Model;

class User extends Model
{
protected $connection = [
// Database type
'type' => 'mysql',
// Database Connection DSN Configuration
'dsn' => '',
// server address
'hostname' => '127.0.0.1',
// Database name
'database' => 'thinkphp',
// Database username
'username' => 'root',
// Database password
'password' => '',
// Database Connection Port
'hostport' => '',
// Database Connection Parameters
'params' => [],
// Database coding defaults to utf8
'charset' => 'utf8',
// Database table prefix
'prefix' => 'think_',
];
}
//DSN strings can also be used to define, for example:
//Setting database connection information separately in the model
namespace app\index\model;

use think\Model;

class User extends Model
{
//Or use string definitions
protected $connection = 'mysql://root:1234@127.0.0.1:3306/thinkphp#utf8';
}
//It also supports configuration names that directly use database connections, such as:
//Setting database connection information separately in the model
namespace app\index\model;

use think\Model;

class User extends Model
{
// Direct use of configuration parameter names
protected $connection = 'db_config1';
}

Keywords: Database PDO MySQL PHP

Added by DarkHavn on Sat, 24 Aug 2019 15:49:09 +0300