Follow this Blog!

Jul 1, 2015

f Comment

[SOLVED] Yii: Fixing "could not find driver" and "exception 'CDbException' with message 'CDbConnection failed to open the DB connection.'"

Amazon If you use the Yii PHP framework, you may get this error when running a PHP script from the command line.
exception 'CDbException' with message 'CDbConnection failed to open the DB connection.' in ..\yii-1.1.13\framework\db\CDbConnection.php:387
Stack trace:
#0 ..\yii-1.1.13\framework\db\CDbConnection.php(330): CDbConnection->open()
#1 ..\yii-1.1.13\framework\db\CDbConnection.php(308): CDbConnection->setActive(true)
#2 ..\yii-1.1.13\framework\base\CModule.php(387): CDbConnection->init()
#3 ..\yii-1.1.13\framework\base\CApplication.php(438): CModule->getComponent('db')
#4 ..\yii-1.1.13\framework\db\ar\CActiveRecord.php(623): CApplication->getDb()
#5 ..\yii-1.1.13\framework\db\ar\CActiveRecord.php(2309): CActiveRecord->getDbConnection()
#6 ..\yii-1.1.13\framework\db\ar\CActiveRecord.php(387): CActiveRecordMetaData->__construct(Object(Entry))
#7 ..\chtoen\protected\models\Entry.php(57): CActiveRecord::model('Entry')
#8 ..\chtoen\protected\script\CheckForDbErrors.php(24): Entry::model()
#9 {main}
In your yii log, you may see this error message:
[error] [exception.CDbException] could not find driver
[error] [exception.CDbException] exception 'CDbException' with message 'CDbConnection failed to open the DB connection.' in ..\yii-1.1.13\framework\db\CDbConnection.php:387
Stack trace:
#0 ..\yii-1.1.13\framework\db\CDbConnection.php(330): CDbConnection->open()
#1 ..\yii-1.1.13\framework\db\CDbConnection.php(308): CDbConnection->setActive(true)
#2 ..\yii-1.1.13\framework\base\CModule.php(387): CDbConnection->init()
#3 ..\yii-1.1.13\framework\base\CApplication.php(438): CModule->getComponent('db')
#4 ..\yii-1.1.13\framework\db\ar\CActiveRecord.php(623): CApplication->getDb()
#5 ..\yii-1.1.13\framework\db\ar\CActiveRecord.php(2309): CActiveRecord->getDbConnection()
#6 ..\yii-1.1.13\framework\db\ar\CActiveRecord.php(387): CActiveRecordMetaData->__construct(Object(Entry))
#7 ..\chtoen\protected\models\Entry.php(57): CActiveRecord::model('Entry')
#8 ..\chtoen\protected\script\CheckForDbErrors.php(24): Entry::model()
#9 {main}
Let's assume the script you are running is CheckForDbErrors.php and you are running MySQL database.

Possible cause #1

In the beginning of CheckForDbErrors.php, you should have a line of code similar to the following:

Yii::createConsoleApplication('../config/main.php');

This line tells Yii that CheckForDbErrors.php is meant to run in the console, the terminal, or the command line. The main.php configuration file must define 'db' which looks like the following:
'db'=>array(
    'connectionString' => 'mysql:host=localhost;dbname=myDb',
    'emulatePrepare' => true,
    'username' => 'root',
    'password' => 'rootPassword',
    'charset' => 'utf8',
),
If this doesn't fix the error, read on.

Possible cause #2

What's the PHP executable are you using? In my case, I run "which php" to find out that I am running /drives/c/wamp2.5/bin/php/php5.5.9/php. Edit /drives/c/wamp2.5/bin/php/php5.5.9/php.ini to enable each MySQL-related extension such as the following:

extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll

Save the changes and try again.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael