php 连接 postgresql 和 codeigniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29630851/
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
connecting postgresql and codeigniter
提问by Limon
I'm new using postgresql and I've been using Codeigniter for a year.
我是使用 postgresql 的新手,我已经使用 Codeigniter 一年了。
I have a small postgresql database and I wanna call it from Codeigniter.
我有一个小的 postgresql 数据库,我想从 Codeigniter 调用它。
In my database.php file I have this setup:
在我的 database.php 文件中,我有这样的设置:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'pgsql:host=localhost;port=5432;dbname=test;user=postgres;password=aPass',
// 'dsn' => '',
'hostname' => 'localhost',
'username' => 'postgres',
'password' => 'aPass',
'database' => 'test',
'dbdriver' => '',
'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
// 'port' => '5432'
);
In a controller I have this function:
在控制器中,我有这个功能:
public function dbtest(){
$this->load->database();
$feeds = $this->db->get('vts_feeds');
echo $feeds;die();
}
The result I'm getting is:
我得到的结果是:
An Error Was Encountered
You have not selected a database type to connect to.
Why is it not working?
为什么它不起作用?
回答by Always Sunny
Try to set,
尝试设置,
dbdriver - The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case.
dbdriver - 数据库类型。即:mysql、postgres、odbc 等。必须用小写指定。
More Info: https://www.codeigniter.com/user_guide/database/configuration.html
更多信息:https: //www.codeigniter.com/user_guide/database/configuration.html
EDIT:Try this config for PDO
in postgres
编辑:PDO
在 postgres 中试试这个配置
$db['default']['hostname'] = 'pgsql:host=localhost;dbname=yourdb'; //set host
$db['default']['username'] = 'your username'; //set username
$db['default']['password'] = 'your password'; //set password
$db['default']['database'] = 'your database'; //set databse
$db['default']['dbdriver'] = 'pdo'; //set driver here
回答by Mike Miller
Remove the 'dsn' string and change to 'dbdriver' => 'postgre'
in the config array.
删除 'dsn' 字符串并更改为'dbdriver' => 'postgre'
配置数组中的 。
Despite the CI docs stating the value should be 'postgres'
if you check the directory and file names in system>database>drivers
the folder is actually called 'postgre'
, the file name 'postgre_driver.php'
and the class 'CI_DB_postgre_driver'
so we can assume the docs are wrong here. Weird this hasnt raised its ugly head before
尽管 CI 文档说明值应该是'postgres'
如果您检查文件system>database>drivers
夹中的目录和文件名实际上被调用'postgre'
,文件名'postgre_driver.php'
和类,'CI_DB_postgre_driver'
所以我们可以假设文档在这里是错误的。奇怪,这之前还没有抬起它丑陋的头
回答by Fábio Mendon?a
In the file database.php
where it reads:
在database.php
它读取的文件中:
'dbdriver' => ' ',
Put the following argument:
提出以下论点:
'dbdriver' => 'pgsql',
回答by Coordinate35
I just did it right now. You just leave :
我刚刚做到了。你只要离开:
$db['default']['dsn'] = '';
And set:
并设置:
$db['default']['dbdriver'] = 'postgre';
回答by Kamran
I had the same problem. Try these steps
我有同样的问题。试试这些步骤
- don't remove
dsn
string addport=>5432
to your config array - set dbdriver to
'dbdriver' => 'postgres'
- check your php.ini file if postgre extension is enabled or not
extension=php_pdo_pgsql.dll
- 不要删除
dsn
字符串添加port=>5432
到您的配置数组 - 将 dbdriver 设置为
'dbdriver' => 'postgres'
- 检查您的 php.ini 文件是否启用了 postgre 扩展
extension=php_pdo_pgsql.dll