如何在json中显示数据库名称

问题描述 投票:-2回答:1

我从mysql获取值到json。现在我的所有数据都正常显示我想在json中显示数据库名称。任何帮助都可以得到赞赏。

json.php代码:

       <?php   
       $connect = mysqli_connect("localhost", "root", "", "recruiter");  
       $sql = "SELECT * FROM recruiter";  
       $result = mysqli_query($connect, $sql);  
       $json_array = array();  
       while($row = mysqli_fetch_assoc($result))  
       {  
            $json_array[] = $row;

       }  
       echo (json_encode($json_array));     
       ?>  

输出:

{
    "here i want db name":[
        {
            "job_id":"1",
            "job_title":"Java developer",
            "job_description":"Java description",
            "job_details":"details",
            "job_skills":"java",
            "job_min_exp":"0 Yr",
            "job_max_exp":"2 Yrs",
            "company_name":"",
            "job_location":"",
            "industry":"",
            "department":"",
            "job_role":"",
            "owner_name":"",
            "owner_mobile":"",
            "owner_email":"",
            "owner_description":" "
        }
    ]
}
php mysql json
1个回答
3
投票

根据您的输出,理解数据库名称是多维数组下的数组。在JSON中显示数据库名称。

试试这个代码

<?php   
   $database_name = "recruiter";
   $connect = mysqli_connect("localhost", "root", "", $database_name);  
   $sql = "SELECT * FROM recruiter";  
   $result = mysqli_query($connect, $sql);  
   $json_array = array();  
   while($row = mysqli_fetch_assoc($result))  
   {  
        $json_array[] = $row;

   }  

   echo json_encode(array($database_name => array($json_array)));
?>  
© www.soinside.com 2019 - 2024. All rights reserved.