Home > Article > PHP Framework > How to add database connection in yii
To access the database, you first need to establish a connection to it by creating a yii\db\Connection instance.
(Related article tutorial recommendation: yii framework)
Because database connections often need to be used in multiple places, a common The best way is to configure it as an application component, as follows:
return [ // ... 'components' => [ // ... 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=example', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], ], // ... ];
After that, you can use the database connection through the statement Yii::$app->db
.
For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of How to add database connection in yii. For more information, please follow other related articles on the PHP Chinese website!