How to connect Yii Framework 2.0 to database

In this tutorial I will show you how you can easily connect Yii Framework 2.0 to database. You have first create a new database if you don't have. Then go to your Yii project and open the config/db.php file.

In your config/db.php file you will find out this code:

<?php

return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic', 
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];

You must rename yii2basic with the name of your database.

Also, you must rename root with your username. And don't forget to add a password if you use a password for your database.

Video tutorial:

In my next tutorials I will show you how you can use Mysql with Yii Framework 2.0.









Leave a Comment