Home  >  Article  >  PHP Framework  >  How to add database connection in yii

How to add database connection in yii

王林
王林Original
2020-02-19 14:49:442295browse

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.

How to add database connection in yii

(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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn