How To Use A Db Database Connection

Framework \ db \ CDbConnection. PHP

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

:blink:

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.

YES

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

http://www.yiiframework.com/doc/guide/1.1/en/database.dao

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(

        $config['components']['db']['connectionString'],

        $config['components']['db']['username'],

        $config['components']['db']['password'],

        array(

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

        )

    );


} catch (PDOException $e) {

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

}