Php ci framework query mysql returns json array

the effect to be obtained is
[{"name": "aaa", "id": "1", "sex", "man"}, {"name": "bbb", "id": "2", "sex", "woman"}, {}.]

now the display is
{"name": "aaa", "id": "1", "sex", "man"} {"name": "bbb", "id": "2", "sex", "woman"} {}.

my code:

<?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);
        }   
    }
}

how should I modify it?

Mar.07,2021

public function index() {
   $this->load->database();
   $query = $this->db->query('SELECT name, id, sex FROM user');
   echo json_encode($query);
}

is that all right soon


resolved

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Demo extends CI_Controller {

    public function index() {
       $con=mysqli_connect("localhost","root","***","test"); 
       // mysqli_query($con,"SET NAMES utf8");
       $query = mysqli_query($con,"select * from user");
       $array =  array();
       while($row = mysqli_fetch_assoc($query)){
        $array[] =  $row; 
      }
      echo json_encode($array);

    }
} 
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9b0bf-1825.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9b0bf-1825.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?