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

做~自己de王妃 提交于 2019-12-08 00:57:48

问题


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?


回答1:


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




回答2:


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




回答3:


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




回答4:


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...




回答5:


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.




回答6:


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.




回答7:


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'



回答8:


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



来源:https://stackoverflow.com/questions/20401228/cdbconnection-failed-to-open-the-db-connection-could-not-find-driver-in-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!