Codeigniter和从模型中提取数据

时间:2014-02-08 18:34:14

标签: php codeigniter

您好我是MVC和Codeigniter的新手,我遇到了障碍,我已经构建了一个登录系统,现在我想从名为post的模型中的查询中提取数据

post型号的代码

class Post extends CI_MODEL {

    function stream($uid, $updated, $limit) {
        //SELECT * FROM vPAS_Posts_Users_Temp WHERE post_user_id =:id AND post_type !=4 AND post_updated >:updated AND post_updated <:now UNION SELECT u.* FROM vPAS_Posts_Users_Temp u JOIN PAS_Follow f ON f.folw_followed_user_id = u.post_dynamic_pid WHERE u.post_updated >:updated AND post_updated <:now AND (( f.folw_follower_user_id =:id AND f.folw_deleted = 0 ) OR ( u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by =:id AND u.post_user_id !=:id AND u.post_type =4 )) ORDER BY post_posted_date DESC LIMIT :limit
        $now = microtime(true);
        $sql = "
            SELECT 
                * 
            FROM 
                vPAS_Posts_Users_Temp 
            WHERE 
                post_user_id = ? 
                AND post_type !=4 
                AND post_updated > ?
                AND post_updated < ? 
            UNION 

            SELECT 
                u.* 
            FROM 
                vPAS_Posts_Users_Temp u 
            JOIN 
                PAS_Follow f 
                ON f.folw_followed_user_id = u.post_dynamic_pid 
                WHERE u.post_updated > ?
                AND post_updated < ? 
                AND (( f.folw_follower_user_id = ? AND f.folw_deleted = 0 ) 
                OR ( u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by = ? AND u.post_user_id != ? AND u.post_type =4 )) 
            ORDER BY 
                post_posted_date DESC 
            LIMIT ?
        "; 

        $query = $this->db->query($sql, array($uid, $updated, $now, $updated, $now, $uid, $uid, $uid, $limit));

        return $query->result_array();

    }

}

我想将结果提取到我的home控制器并使用它们,我已经在home控制器中写了这个

function index() {
   if($this->session->userdata('logged_in')) {

     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['username'];
     $data['first_name'] = $session_data['first_name'];
     $data['last_name'] = $session_data['last_name'];
     $this->load->library('gravatar');
     $data['gravatar'] = $this->gravatar->get_gravatar($session_data['username']);

     //Stream Data
     $updated = 0.0;
     $limit = 50;
     $uid = $session_data['id'];

     $this->load->model('post');
     $data['results'] = $this->post->stream($uid, $updated, $limit);

     $this->load->view('home_view', $data);
   } else {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
}

然而,当我在我的视图中echo json_encode($results);时,我只是返回了[],有人可以指出我正确的方向。

1 个答案:

答案 0 :(得分:0)

在使用@Phil建议后,我使用$this->db->last_query();并发现查询所需的其中一个变量不正确。

对Codeigniter不熟悉我还没有看到所有的调试功能。感谢堆栈帮助我理解并解决了我的问题。