猿问

php ci框架 查询mysql 返回json数组

要获得的效果是:
[{"name":"aaa","id":"1","sex","man"},{"name":"bbb","id":"2","sex","woman"},{}...]

现在的显示是
{"name":"aaa","id":"1","sex","man"}{"name":"bbb","id":"2","sex","woman"}{}...

我的代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Demo extends CI_Controller {
    public function index() {
       $this->load->database();
       $query = $this->db->query('SELECT name, id, sex FROM user');

       foreach ($query->result() as $row){
       echo json_encode($row);
        }   
    }
}

我该如何修改?

慕仙森
浏览 613回答 2
2回答

UYOU

public function index() { $this->load->database(); $query = $this->db->query('SELECT name, id, sex FROM user'); echo json_encode($query); } 这样不久可以了吗
随时随地看视频慕课网APP
我要回答