php CodeIgniter PDO 数据库驱动程序不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11054618/
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
CodeIgniter PDO database driver not working
提问by James Dawson
I'm trying to use the PDO MySQL driver in my CodeIgniter application. This is my database config:
我正在尝试在我的 CodeIgniter 应用程序中使用 PDO MySQL 驱动程序。这是我的数据库配置:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'testdatabase';
$db['default']['dbdriver'] = 'pdo';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
However, I'm getting this error when I load a controller:
但是,当我加载控制器时出现此错误:
Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in C:\xampp\htdocs\testsite\system\database\drivers\pdo\pdo_driver.php:114
致命错误:在 C:\xampp\htdocs\testsite\system\database\drivers\pdo\pdo_driver.php:114 中出现消息“无效数据源名称”的未捕获异常“PDOException”
I've checked the data source using die($this->hostname);in pdo_driver.phpand it's coming out as:
我已经使用die($this->hostname);in检查了数据源pdo_driver.php,结果如下:
localhost;dbname=testdatabase
so it is getting the correct database name. The database exists and I do have MySQL running.
所以它得到了正确的数据库名称。数据库存在并且我确实运行了 MySQL。
What could be going wrong here? Thank you.
这里可能出了什么问题?谢谢你。
采纳答案by Mike Mackintosh
This should not be the case.
这不应该是这种情况。
localhost;dbname=testdatabase
should be
应该
mysql:dbname=testdatabase;host=localhost;
回答by MrGusMuller
On file /application/config/database.phpwhere is
在文件/application/config/database.php上
$db['default']['hostname'] = 'localhost';
must be
必须是
$db['default']['hostname'] = 'mysql:host=localhost';
localhostor your database host.
本地主机或您的数据库主机。
回答by Yusuf
PDO driver require a full DSN string to be provided. The string like this
PDO 驱动程序需要提供完整的 DSN 字符串。像这样的字符串
'dsn' = ‘mysql:host=localhost;dbname=databasename';
when you use this string you should remove host and databasename value from array. I think following example gives you an idea.
当您使用此字符串时,您应该从数组中删除主机和数据库名值。我认为下面的例子给了你一个想法。
$db['default'] = array(
'dsn' => 'mysql:host=localhost;dbname=codeigniter3',
'hostname' => '',
'username' => 'root',
'password' => '',
'database' => '',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Thanks
谢谢
回答by Harry
According to CodeIgniter's Database Configurationpage,
根据 CodeIgniter 的数据库配置页面,
For the PDO driver, you should change the 'hostname => 'localhost'' to "'hostname' => mysql:host=localhost" like below:
对于 PDO 驱动程序,您应该将 ' hostname => 'localhost''更改为 " 'hostname' => mysql:host=localhost",如下所示:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysql:host=localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
.....
.....
);

