php CDbConnection 打开数据库连接失败:在 Yii 中找不到驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20401228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:10:07  来源:igfitidea点击:

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

phpmysqlyiizend-studioyii-cactiverecord

提问by ackuser

while trying to connect with mysql in yii framework it shows

在尝试与 yii 框架中的 mysql 连接时,它显示

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

“CDbConnection 无法打开数据库连接:找不到驱动程序”错误

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

我的 php 驱动程序已经启用,但它显示相同的错误

Im using zend studio, zend server for this

我为此使用了 zend studio,zend 服务器

how could i fix this?

我怎么能解决这个问题?

回答by ackuser

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

我正在使用 Linux Mint ..在我的情况下我需要

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

因为我正在将我的环境从 Lampp 迁移到 Apache + PHP + MySQL,我不得不在 php.ini 中更改很多内容

回答by ackuser

Things you should check

您应该检查的事项

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

首先,您应该检查 phpinfo() 是否安装了 pdo_drivers。

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

如果你使用 IIS 服务器,你应该检查是否安装了 php 扩展。

check php.ini for extensions_dir is properly given

检查 php.ini for extensions_dir 是否正确给出

check you ext folder in php contains pdo_mysql dlls

检查 php 中的 ext 文件夹是否包含 pdo_mysql dll

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

有时问题可能是由于安装了 zend 服务器。您应该检查 zend 服务器是否正确使用了 php 扩展

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

之后使用以下函数检查简单的 php-mysql 连接

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

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

如果可行,则使用 Yii、Slim、Zend php-mysql 连接

回答by Dan Santana

I'm using Linux Ubuntu.

我正在使用 Linux Ubuntu。

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

我有同样的问题,但就我而言,我必须安装 php-mysql。

sudo apt-get install php5-mysql

sudo apt-get install php5-mysql

and restart the service

并重启服务

sudo service apache2 restart

I hope thats help someone

我希望那能帮助某人

回答by kurmi

try to install Sqlitedrive by using following command in Ubuntu

尝试Sqlite在 Ubuntu 中使用以下命令安装驱动器

sudo apt-get install sqlite php5-sqlite

Now restart the Apache server by using following command

现在使用以下命令重新启动 Apache 服务器

service apache2 restart

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

这两个步骤解决了我的问题。希望它也能解决其他人的...

回答by Денис Ленцов

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.

就我而言,我还必须启用 pdo_sqlite 模块,这解决了问题。它在 HOSTiq.com.ua 托管上。我想它可以为某人节省几个小时的搜索时间。

回答by rapttor

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

如果未设置 connectionID,db 缓存默认为 /protected/data 目录中的 mysqli 数据库,如果系统上未安装 mysqli 驱动程序,则无法访问该目录(专用服务器的常见问题、DO droplets、xampp/wamp...)

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

或者,您可以禁用数据库缓存并启用文件缓存。

回答by ackuser

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

我发现谷歌搜索是唯一对我有用的

回答by Artur K?dzior

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

在当前版本的 Yii 中,您有一个目录requirements。它将启动Yii Requirement Checker并显示缺少的内容。

After that get you connection string right in main.php

之后在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

就我而言,这是非常愚蠢的事情。我忘了mysql:

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