Pages

Monday, December 9, 2013

Connect to a MySQL Database in Yii

Configuring MySQL Database in Yii

Before we continue the discussion with the other features in Gii, let me show you how to configure your Yii Web Application to connect to a MySQL database. Yii's Data Access Objects (DAO) was built on top of the PHP Data Objects (PDO) extension. All the supported database management systems (DBMS) are encapsulated behind a single uniform interface. This means that the code can remain database independent and the application developed using Yii DAO can be easily switched from one DBMS to another without the need for modification.

Let us assume that we are using MySQL Database. You must open the /protected/config/main.php file and uncomment the MySQL script below:

'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=<database-name.',
            'emulatePrepare' => true,
            'username' => '[your-db-username]',
            'password' => '[your-db-password]',
            'charset' => 'utf8',
        ),

Also, do not forget to comment the SQL connection script that was configured once the web application was created via yiic. See the script below
/*
'db'=>array(
            'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
*/

No comments:

Post a Comment