最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

CodeIgniter 2.1.4 model 得不到数据库数据 model db 类无 result_array()方法

XAMPP下载 admin 835浏览 0评论

为了尝鲜,在 ci 中国社区下载了2.1.4的版本,因为 en 文读起来还是略微费劲.

 

所以在中国社区版的 online 文档里找了几个例子先尝试下.

 

但随后问题就来了.

 

配置完数据库、建立模型后,ci 可以连接数据库但确无法读取数据,也未报错。

 

几番尝试,因例子是社区文档里直接复制过来的,应不会有错。

但程序走到这里确实停了,

 

  1. public function gets_news($slug = FALSE) {
  2. if ($slug == FALSE) {
  3. $query = $this->db->get ( ‘news’ );
  4. return $query->result_array ();
  5. }
  6. $query = $this->db->get_where ( ‘news’, array (
  7. ‘slug’ => $slug
  8. ) );
  9. return $query->row_array ();
  10. }

 

 

 

便想到是版本的问题,在 ci 中文社区里找了个2.1.1版本,一试果然成功,那么毫无疑问的是版本问题了,那么问题在哪呢。

最后,在 model 里写了这个方法:

 

  1. public function getMethodsList() {
  2. $class_methods = get_class_methods ( $this->db );
  3. foreach ( $class_methods as $method_name ) {
  4. echo “$method_name” . “</br>”;
  5. }
  6. }

 

 

查找了下,果然没有例子里的result_array()和 row_array()方法,再翻过头来看2.1.4原包里的 user_guide 里面关于 model 一节,果然改变了用法,因此上面的查询方法改成如下:

 

  1. public function get_news($slug = FALSE) {
  2. if ($slug == FALSE) {
  3. $query = $this->db->get ( ‘news’ );
  4. $result = $query->result ();
  5. return $result;
  6. }
  7. $query = $this->db->get_where ( ‘news’, array (
  8. ‘slug’ => $slug
  9. ) );
  10. return $query->result ();

转载请注明:XAMPP中文组官网 » CodeIgniter 2.1.4 model 得不到数据库数据 model db 类无 result_array()方法

您必须 登录 才能发表评论!