Tuesday, November 8, 2011

Configuration Yii Framework to MySQL Database

By default Yii Framework to connect to SQLite database. It can take a look at webappname \ protected \ config \ main.php. Consider the following code:
*/
'db'=>array(
 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
 'connectionString' => 'mysql:host=localhost;dbname=testdrive',
 'emulatePrepare' => true,
 'username' => 'root',
 'password' => '',
 'charset' => 'utf8',
),
*/

In the code above shows the application to connect to SQLite database where there are no tag "comment" while in MySQL there is a tag "comment" which means the code will not be processed. We need to do is

  1. Adding a tag comment / * and * / on SQLite SQLite database that is not executed. Consider the following code:
    /*
    'db'=>array(
     'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
    ),
    */
  2. Remove the tag comment / * and * / to MySQL database and rename testdrive.db (default) with the name of your database. example yiicode_db
    // Using MySQL database
    'db'=>array(
     'connectionString' => 'mysql:host=localhost;dbname=yiicode_db',
     'emulatePrepare' => true,
     'username' => 'root',
     'password' => '',
     'charset' => 'utf8',
    ),
So the whole database connection code would look something like the following:
/*
'db'=>array(
 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
*/
// Using MySQL database
'db'=>array(
 'connectionString' => 'mysql:host=localhost;dbname=yiicode_db',
 'emulatePrepare' => true,
 'username' => 'root',
 'password' => '',
 'charset' => 'utf8',
),

14 comments:

  1. Thanks For This,

    But i want to store forms data dynamically in to database how can i achieve this.

    If any one know about this then please send the links or docs to sk.ijaz@gmail.com

    ReplyDelete
  2. Hii,
    Nice blog and nice post.Thank you so much for this post.Iam also looking for this .Have a loook on yiioverflow if you want any software solutions.

    ReplyDelete
  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete

Yii Framework Tutorials