用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5937|回复: 2
收起左侧

codeigniter使用odbc连接Access数据库的方法

[复制链接]
发表于 2011-1-12 10:56:59 | 显示全部楼层 |阅读模式
本帖最后由 dodosong 于 2011-6-8 09:54 编辑

使用CI连接Access的时候报错
Fatal error: Call to undefined method CI_DB::CI_DB() in system/database/drivers/odbc/odbc_driver.php on line 53


搜了半天,看到这个帖子,http://codeigniter.org.cn/forums/thread-4031-1-1.html . 同样设置后发现还是连接不上.没办法,只好在老外论坛上找找看了,不过还真找到了英语不咋滴,不过还是理解怎么操作了.简单说下:

1. 到 system\database\drivers\odbc\目录下, 找到odbc_driver.php, 注释掉51到56行
2. 不能用$query->result()了, 请用$query->result_array() 代替.
3. $query->num_rows()也不管用了. 想其他办法吧.
4. 把数据库文件放在 index.php 同目录下的database文件夹下。
5. database.php里面配置方法为

PHP复制代码
 
 
$active_group = "default";
$active_record = TRUE;
 
$db['default']['hostname'] = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath("database/aa.mdb");
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = realpath("database/aa.mdb");
$db['default']['dbdriver'] = "odbc";
$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";
复制代码



虽然不完美,但是我成功连上了,而且操作简单.感谢这位Nick.

chrissie1982 - 09 October 2009 06:32 AM
I had the same error. after hours and hours i got it work in the following way:

1. go to the system\database\drivers\odbc\odbc_driver.php and comment out the whole function CI_DB_odbc_driver($params) line 51 to line 56 -> this seems to be a bug in the constructor.

2. do not use statements like (foreach $query->result() as $row){ echo $row->ID;} ... use instead (foreach $query->result_array() as $row){ echo $row[‘ID’];} this works for me.

3. i don’t exactly know if there’s another mistake within codeigneter or php with odbc. i can’t use $query->num_rows(). this always gives back -1 no matter if there’s a record selected or not. i use instead a seperate counter like $count = 0; foreach (....){count++;}

hope i could help you, ‘cause i was sitting for about 2 days on that problem but now it works.

Chris

Thanks for figuring this out Chris. I am connecting to an MSSQL instance and was able to get the class to work with this.

I was able to get all the other functions working properly by setting

var $_random_keyword = ' ASC';
Nick

原帖地址http://codeigniter.com/forums/viewthread/128295/#691312

评分

参与人数 1威望 +5 收起 理由
Hex + 5 很给力!

查看全部评分

 楼主| 发表于 2011-2-15 15:03:28 | 显示全部楼层
错误:Fatal error: Call to undefined method CI_DB::CI_DB() in system/database/drivers/odbc/odbc_driver.php on line 53
Line 53 being
parent::CI_DB($params);
替换为
parent::CI_DB_driver($params);
solves the issue.
发表于 2011-5-2 18:57:19 | 显示全部楼层
总算找到了.!

本版积分规则