为了尝鲜,在 ci 中国社区下载了2.1.4的版本,因为 en 文读起来还是略微费劲.
所以在中国社区版的 online 文档里找了几个例子先尝试下.
但随后问题就来了.
配置完数据库、建立模型后,ci 可以连接数据库但确无法读取数据,也未报错。
几番尝试,因例子是社区文档里直接复制过来的,应不会有错。
但程序走到这里确实停了,
-
public function gets_news($slug = FALSE) {
-
if ($slug == FALSE) {
-
$query = $this->db->get ( ‘news’ );
-
return $query->result_array ();
-
}
-
$query = $this->db->get_where ( ‘news’, array (
-
‘slug’ => $slug
-
) );
-
return $query->row_array ();
-
}
便想到是版本的问题,在 ci 中文社区里找了个2.1.1版本,一试果然成功,那么毫无疑问的是版本问题了,那么问题在哪呢。
最后,在 model 里写了这个方法:
-
public function getMethodsList() {
-
$class_methods = get_class_methods ( $this->db );
-
foreach ( $class_methods as $method_name ) {
-
echo “$method_name” . “</br>”;
-
}
-
}
查找了下,果然没有例子里的result_array()和 row_array()方法,再翻过头来看2.1.4原包里的 user_guide 里面关于 model 一节,果然改变了用法,因此上面的查询方法改成如下:
-
public function get_news($slug = FALSE) {
-
if ($slug == FALSE) {
-
$query = $this->db->get ( ‘news’ );
-
$result = $query->result ();
-
return $result;
-
}
-
$query = $this->db->get_where ( ‘news’, array (
-
‘slug’ => $slug
-
) );
-
return $query->result ();
转载请注明:XAMPP中文组官网 » CodeIgniter 2.1.4 model 得不到数据库数据 model db 类无 result_array()方法