CDbConnection failed to open the DB connection: could not find driver in Yii

52.9k views Asked by At

while trying to connect with mysql in yii framework it shows

"CDbConnection failed to open the DB connection: could not find driver " error

php code :

     'db'=>array(
        'class' => 'CDbConnection',
        'connectionString' => 'mysql:host=localhost:3306;dbname=testdrive',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => 'root',
        'charset' => 'utf8',
    ), 

my php drivers are already enabled , but it shows the same error

Im using zend studio, zend server for this

how could i fix this?

8

There are 8 answers

1
AudioBubble On BEST ANSWER

Things you should check

First you should check phpinfo() for the pdo_drivers installed.

if u use IIS server , you should check php extensions installed or not.

check php.ini for extensions_dir is properly given

check you ext folder in php contains pdo_mysql dlls

Some times the problem may occur due to zend server installation .You should check zend server is properly using the php extensions

After that check simple php-mysql connection using the following functions

mysql_connect('localhost:3306','root','root');
mysql_select_db('testdrive');

If it works then go with Yii, Slim, Zend php-mysql connections

0
rapttor On

My solution was this:

    cache' => array(
        'class' => 'system.caching.CDbCache',
        //'class' => 'system.caching.CFileCache',
        'connectionID'=>'db', // <<< THIS IS THE ISSUE
    ),

if connectionID is not set, db caching defaults to mysqli database in /protected/data directory which cannot be accessed if mysqli driver is not installed on system (common issue with dedicated servers, DO droplets, xampp/wamp...)

or, you can disable db caching and enable fileCache instead.

0
Dan Santana On

I'm using Linux Ubuntu.

I have the same problem, but in my case i have to install the php-mysql.

sudo apt-get install php5-mysql

and restart the service

sudo service apache2 restart

I hope thats help someone

0
ackuser On

I am using Linux Mint..In my case I needed

apt-get install php5-mysql 

because I was moving my environment from Lampp to Apache + PHP + MySQL and I had to change a lot of things in the php.ini

0
Artur Kedzior On

In the current version of Yii you have a directory requirements. It will launch Yii Requirement Checker and will show you what is missing.

After that get you connection string right in main.php

'db'=>array(
            'connectionString' => 'mysql:host=deathstar;port=3306;dbname=database',
            'emulatePrepare' => true,
            'username' => 'user',
            'password' => 'pass',
            'charset' => 'utf8',
            'tablePrefix' => 'tbl_'
),

In my case it was something very silly. I forgot mysql: in

'connectionString' => 'mysql:host=deathstar;port=3306;dbname=database'
0
ackuser On

If you don't get it only with this

apt-get install php-mysql

you can try this

apt-get install php-pear php5-dev libmysqlclient15-dev
pecl install pdo
pecl install pdo_mysql

I've found googling it was the only that works fine for me

0
kurmi On

try to install Sqlite drive by using following command in Ubuntu

sudo apt-get install sqlite php5-sqlite

Now restart the Apache server by using following command

service apache2 restart

this two step solve my problem. hope it will solve other's also...

0
Денис Ленцов On

In my case, I also had to enable pdo_sqlite module and this solved the problem. It was on a HOSTiq.com.ua hosting. I suppose it could save someone a few hours on searching.