从MySQL返回带有列名称的JSON [英] Return JSON from MySQL with Column Name
本文介绍了从MySQL返回带有列名称的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用PHP从MySQL中获取一行,然后使用以下代码将其编码为JSON
I use PHP to fetch a row from MySQL and then encode it into JSON using the following code
$jsonData = array();
if(mysqli_num_rows($result) > 0){
while ($array = mysqli_fetch_row($result)) {
$jsonData[] = $array;
}
$json = json_encode($jsonData);
echo stripslashes($json);
}`
但是,我仅获得行值.我想要行值及其列名.当前,它返回以下JSON.
However, I only get the the row values. I want rows values along with their column names. Currently it returns the following JSON.
[["shekhar","Shekhar Chatterjee","https://graph.facebook.com/1254850974526564/picture","0"]],[["shek","Shekhar Chatterjee","","0"]]
我想要以下输出:
[{
"user":"shekhar",
"name":"Shekhar Chatterjee",
"url":"https://graph.facebook.com/1254850974526564/picture",
"stat":"0"
},{
"user":"shekhar",
"name":"Shekhar Chatterjee",
"url":"https://graph.facebook.com/1254850974526564/picture",
"stat":"0"
}]
推荐答案
使用mysqli_fetch_assoc()
您在这里
$jsonData = array();
if(mysqli_num_rows($result) > 0){
while ($array = mysqli_fetch_assoc($result)) {
$jsonData[] = $array;
}
$json = json_encode($jsonData);
echo stripslashes($json);
}
这篇关于从MySQL返回带有列名称的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文