posted by kevin on June 9, 2009

Since I have had quite a bit of traffic to my site from people searching 'Yii MySQL', 'Yii Framework MySQL' 'Connecting Yii To MySQL' and others I figured I'd accommodate you guys with a quick 'How-to' series. This is part one and simply covers getting your MySQL connection working using Yii.

Lets get started...

Before being able to use MySQL properly in Yii you must have the MySQL PDO extension installed.

After doing that you have to snap-in the MySQL connector into your configuration file.

Assuming you started your Yii application using the command-line tool you should have a file in protected/config/main.php.

 
<?php
//File: protected/config/main.php
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
 
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'PHP/MySQL Development',
    'defaultController'=>'post',
 
    // preloading 'log' component
    'preload'=>array('log'),
 
    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
 
    ),
 
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>require(dirname(__FILE__).'/params.php'),
 
    // application components
    'components'=>array(
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
            ),
        ),
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            // force 401 HTTP error if authentication needed
            'loginUrl'=>null,
        ),
        'db'=>array(
           'class'=>'CDbConnection',
           'connectionString'=>'mysql:host=myDatabaseHost;dbname=myDatabasename',
            'username' => 'myUsername',
            'password' => 'myPassword'
        ),
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'tag/<tag>'=>'post/list',
                'posts'=>'post/list',
                'post/<id:\d+>'=>'post/show',
                'post/update/<id:\d+>'=>'post/update',
            ),
        ),
        'cache'=>array(
            'class'=>'system.caching.CDbCache',
        ),
    ),
);
 

Now I left most of my configuration in here just to show you where the DB configuration is at in relative terms to whatever else you may have. So just copy the 'db' array, change the parameters to what you use for your mySQL and you should be ready for the next step!

7 comments to "Yii Framework + MySQL Part 1 - Connecting To MySQL"

#27
Jan says:
July 2, 2009 at 01:03 pm
Thx, this helped me a lot getting a quick start!
#59
77 says:
September 3, 2009 at 06:14 pm
Thanks! Big help :)
#71
Febry Indra Setyawan says:
October 27, 2009 at 10:05 pm
what is the meaning of ---> 'defaultController'=>'post' ??? 'cos if i set it into my config (main.php) and i execute the command (yiic shell) to generate model i got the error on console
#72
October 28, 2009 at 08:57 am
The meaning of 'defaultController' => 'post' simply states that if you go to: http://www.yoursite.com/ since no controller is specified like: http://www.yoursite.com/index.php?r=Post, it will use the Post Controller in: /protected/controllers/PostController.php. That shouldn't affect getting an error on the console. What was your error?
#105
February 8, 2010 at 02:02 am
hi sir.. what is the meaning of
 
'post/<id:\d+>'=>'post/show',
 
are "rules" si similiar with routes in codeigniter, sir? thanks
#128
Raj Kumar Goyal says:
May 11, 2010 at 04:16 am
I have done db setting in config/main.php like 'db'=>array( 'class'=>'CDbConnection', 'connectionString'=>'mysql:host=myDatabaseHost;dbname=myDatabasename', 'username' => 'myUsername', 'password' => 'myPassword' ) and I have installed required extension for mysql. but my application is not connecting to the database. Pls help to fix it. Thanks in advance.
#160
July 30, 2010 at 05:55 am
Hi Raj, I think you have to replace the variables above to correspond to yours. For example, replace myDatabaseHost to the name of your host e.g localhost, myDatabasename to the name of the database you want to use e.g accounts, myUsername to your mySQL username e.g root and myPassWord to your password if you have any.
Bookmark and Share

Leave a Comment

Your email address will not be published.

(You can enclose code in <php></php> blocks.)

You may use Markdown syntax.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.