我是codeigniter的新手,并尝试编写一个简单的博客应用程序:
controller:posts.php
class Post extends CI_Model {
function get_posts($num=20, $start=0) {
$this->db->select()->from('posts')->where('active',1)->order_by('date_added','dec')->limit(0,20);
$query = $this->db->get('posts');
return $query->result_array();
}
}
型号:post.php
class Posts extends CI_Controller {
function index() {
$this->load->model('post');
$data['posts'] = $this->post->get_posts();
echo"<pre>";print_r($data['posts']);echo"</pre>";
}
}
我在数据库中填充了posts表。然而,当我试图得到结果时,我得到了这个:
Array
(
)
的MySQL&GT;描述帖子;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------------+-----------------------------+
| postID | int(10) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| post | text | NO | | NULL | |
| date_added | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| userID | int(4) | NO | | NULL | |
| active | tinyint(1) | NO | | NULL | |
| slug | varchar(255) | NO | | NULL | |
+------------+--------------+------+-----+-------------------+------------------
此代码有什么问题,如何解决?
答案 0 :(得分:0)
因为您的查询出错了,因为您已经拥有->from('posts')
,因此您不再需要表格名称
更改
$query = $this->db->get('posts');
使用
$query = $this->db->get();
并且
->order_by('date_added','desc')