How to easily connect database in Yii Framework 1.14

Hi, in this tutorial you will see how to easily connect database in Yii Framework. You first must to create a new database and open protected\config\main.php file.

Next, add this code

'db'=>array(
'class'=>'CDbConnection',
'connectionString'=>'mysql:host=localhost;dbname=testdb',
'username'=>'root',
'password'=>'password',
'emulatePrepare'=>true,
),

Open the protected\views\site\index.php file and connect to mysql by using this code:

$connection=Yii::app()->db;
$command=$connection->createCommand("SELECT * FROM");
$dataReader=$command->query();
foreach($dataReader as $row) {}

Video tutorial:









Leave a Comment