Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Tạo đối tượng json lồng nhau bằng php mysql

Xin chào, hãy thử cái này,

<?php
include '../config/config.php';
if(isset($_GET['sub_cat_id']))
{
    $sub_cat_id = $_GET['sub_cat_id']; 
    $result = mysql_query("SELECT * FROM $questions WHERE sub_cat='$sub_cat_id' ORDER BY level_fk ASC"); 
    $json_response = array(); //Create an array
    while ($row = mysql_fetch_array($result))
    {
        $row_array = array();
        $row_array['qus_pk'] = $row['qus_pk'];        
        $row_array['question'] = $row['question'];
        $row_array['answers'] = array();
        $qus_pk = $row['qus_pk'];  

        $option_qry = mysql_query("SELECT * FROM $qus_ans WHERE qus_pk=$qus_pk");
        while ($opt_fet = mysql_fetch_array($option_qry))
        {
            $row_array['answers'][] = array(
                'options' => $opt_fet['options'],
                'right_ans' => $opt_fet['right_ans'],
            );

        }
        array_push($json_response, $row_array); //push the values in the array
    }
    echo json_encode($json_response);
}
?>    


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. LỖI:Lỗi 1005:Không thể tạo bảng 'cat10e.recording' (errno:150)

  2. Lưu trữ dữ liệu trong MySQL dưới dạng JSON

  3. Câu lệnh được soạn sẵn trong WordPress với điều kiện IN ()

  4. tôi có thể tin tưởng mysql_insert_id () trả về giá trị chính xác không, hoang tưởng đa luồng

  5. Bảng MySQL InnoDB bị hỏng - làm thế nào để khắc phục?