MySQL SELECT查询结果为JSON

时间:2022-08-16 00:12:51

I have a model which runs the following SELECT query.

我有一个运行以下SELECT查询的模型。

MODEL

public function get_details($start, $limit) {
    $sql = $this->db->query("SELECT E.`exam-id` AS `examid`, T.`topic-name` AS `topic`,E.`date`, COUNT(E.`exam-id`) AS `total` FROM `exam` E, `exam-topic` T WHERE E.`exam-dbname`=T.`topic-dbname` GROUP BY `exam-id` ORDER BY `exam-id` DESC LIMIT $start, $limit;");
    if($sql->num_rows() > 0){
        return $sql;
    }
    else{
        return FALSE;
    }
}

MY CONTROLLER

public function get_details() {
    //pagination
    $config =   array(
        'base_url'          =>  base_url()."exam/create/created/index",
        'total_rows'        =>  $this->exam->get_total(),
        'first_link'        =>  "First",
        'last_link'         =>  "Last",
        'per_page'          =>  2,
        'uri_segment'       =>  5,
        'full_tag_open'     =>  "<div class='paginate'>",
        'full_tag_close'    =>  "</div>",
        'num_tag1_open'     =>  '<a>',
        'num_tag1_close'    =>  '</a>',
        'cur_tag_open'      =>  "<a class='current'>",
        'cur_tag_close'     =>  "</a>",
        'next_tag1_open'    =>  "<a>",
        'next_tag1_close'   =>  "</a>",
        'prev_tag1_open'    =>  "<a>",
        'prev_tag1_close'   =>  "</a>",
        'first_tag1_open'   =>  "<a>",
        'first_tag1_close'  =>  "</a>",
        'last_tag1_open'    =>  "<a>",
        'last_tag1_close'   =>  "</a>"
    );
    $page=($this->uri->segment(5) != '') ? $this->uri->segment(5) : 0;
    $this->pagination->initialize($config);

    $data   =   array(
        'query' =>  $this->exam->get_details($page, $config['per_page']),
        'links' =>  $this->pagination->create_links()
    );
    echo json_encode($data);
}

In the view page, I use AJAX to catch the result.

在视图页面中,我使用AJAX来捕获结果。

MY VIEW

function get_details() {
            $.ajax({
                url     :   "<?php echo base_url(); ?>exam/create/created/get_details.html",
                dataType:   "JSON",
                success :   function(data) {
                    var obj = $.parseJSON(data.query);
                    $.each(obj, function(key, val) {
                        alert(key+"\n"+val.uid);
                    });
                }
            });         }

Now, I don't have a clue how to get the results of data[query] in the JSON as data[query] holds multiple results returned by MySQL.

现在,我不知道如何在JSON中获取数据[query]的结果,因为data [query]包含MySQL返回的多个结果。

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

In your controller code should be.

在你的控制器代码应该是。

 $data   =   array(
        'query' =>  json_encode($this->exam->get_details($page, $config['per_page'])),
        'links' =>  $this->pagination->create_links()
    );
echo json_encode($data);

because you are parsing data.query using json parse so data that you are getting from database should be json.

因为你正在使用json解析解析data.query所以你从数据库获得的数据应该是json。

Thanks Amit

#1


1  

In your controller code should be.

在你的控制器代码应该是。

 $data   =   array(
        'query' =>  json_encode($this->exam->get_details($page, $config['per_page'])),
        'links' =>  $this->pagination->create_links()
    );
echo json_encode($data);

because you are parsing data.query using json parse so data that you are getting from database should be json.

因为你正在使用json解析解析data.query所以你从数据库获得的数据应该是json。

Thanks Amit