使用php从数据库获取数据到json

时间:2017-11-18 15:59:11

标签: php json mysqli

我可以通过这种方式从数据库中获取数据;

$con = mysqli_connect("localhost","root","root","demo"); 
$sql = "Select value From Users WHERE name in ('username', 'surname', 'age')"; 
$result = mysqli_query($con,$sql); 

if(mysqli_num_rows($result) > 0 ){ 
    while ($row = mysqli_fetch_assoc($result)) { 
        echo $row["value"]; 
    } 
} 
mysqli_close($con);

输出:

JohnWick42

如何在json中获取此数据

示例json输出:

{"result":[{ 
    "username":"John", 
    "surname":"Wick", 
    "age":"42" 
    } 
]}

感谢。

0 个答案:

没有答案