How To Use A Db Database Connection

Framework \ db \ CDbConnection. PHP

CDbConnection class how to use the db module configuration connection a database ?


Did you mean "how to connect ot DB using db configuration from YII config"?

Maybe you are too urgent to peek into the detailed source code of the framework …

Have you finished reading through the guide? Have you finished one or two tutorial(s) out there?

I think you have to get the big picture of Yii framework before you dig into the source code of it.

And, welcome to the forum.


In your yii app this is done automatically. Just use Yii::app()->db.

If you’re talking about some external files not included in your project, you can do it the same way as usual.

$config = require('../www/protected/config/main.php'); // set the path to required

try {

    $dbh = new PDO(





            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $config['components']['db']['charset'],



} catch (PDOException $e) {

    print "Error: " . $e->getMessage();