du155305
2015-05-25 05:13
浏览 119

Mysql数组转换为json

i have a question i have tryied to make an json response to look like that:

{

"0":{
    "cid":"2",
    "user_name":"4",
    "user_title":null,
    "user_description":null,
    "user_tags":null,
    "user_imgid_logo":"05d419b4cc5c6d1d2fabf90a8dbd967a",
    "user_online":"2",
    "rank":"0",
    "multibitrate":"0",
    "user_name":"gqgeee",
    "user_logo_url":"http:\/\/static.example.tv\/logos\/05d419b4cc5c6d1d2fabf90a8dbd967a.jpg",
    "user_image":"1"
},
"1":{
    "cid":"1434",
    "user_name":"dokumenty",
    "user_title":"*Filmy dokumentalne* (^HQ^-^HD^)",
    "user_description":"short deskt",
    "user_tags":"asfasf",
    "user_imgid_logo":"51bbe341fe3e92f37a89609b80274be8",
    "user_online":"2",
    "rank":"1",
    "multibitrate":"1",
    "user_name":"asdd",
    "user_logo_url":"http:\/\/static.example.tv\/logos\/51bbe341fe3e92f37a89609b80274be8.jpg",
    "user_image":"1"
}}

I have managed to connect to mysql, and create json from database but my json doesnt look as one above:

[

{
    "1":{
        "cid":"1",
        "user_name":"sgsd",
        "user_title":"sgsd asgx",
        "user_description":"asgx na czasie",
        "user_tags":"asgx",
        "user_imgid_logo":"sgsd",
        "user_online":"2",
        "rank":"3",
        "multibitrate":"1",
        "user_name":"Kowalski",
        "user_logo_url":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/84\/asd.png",
        "user_image":"1"
    }
},
{
    "2":{
        "cid":"2",
        "user_name":"sgsd 2",
        "user_title":"sgsd asgx 2",
        "user_description":"asgx na czasie 2",
        "user_tags":"asgx",
        "user_imgid_logo":"sgsd",
        "user_online":"2",
        "rank":"3",
        "multibitrate":"1",
        "user_name":"Kowalski",
        "user_logo_url":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/84\/sgsd.tv_logo_2014.png",
        "user_image":"1"
    }
}

]

^ brackets are not needed and json still is messed up a bit.

this is my souce code of php:

<?php
$getoption = $_GET["option"];
if ($getoption === 'psd') {
//$dataList = substr($dataList, 1, -1);
//open connection to mysql db
$connection = mysqli_connect("localhost","datbase","duser","upass") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from public";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$num_rows = mysql_num_rows($result);
//create an array
//    $emparray[] = array();
//$emparray[] = array('1'=>$row);
echo '<pre>';
var_dump(mysqli_fetch_assoc($result));
echo '</pre>';
$start = 1;
while($row = mysqli_fetch_assoc($result))
{
$emparray[] = array($start=>array($row));
$start++;
}
//$dataList = substr(json_encode($emparray), 1, -1);
$dataList = json_encode($emparray);
echo $dataList;
//close the db connection
mysqli_close($connection);
}
if ($getoption === 'sk') {
echo 'ks os';
}
?>

Would any body willing to help? Thanks.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlaiyin2356 2015-05-25 06:17
    已采纳
    $start = 1;
    while($row = mysqli_fetch_assoc($result))
    {
    $emparray[$start] = $row;
    $start++;
    }
    $dataList = json_encode($emparray);
    echo $dataList;
    

    Try this!! Mostly the part inside the while loop is a bit messed up.

    打赏 评论
  • dongli564510 2015-05-25 05:45

    Please try with this...

    $start = 0;
    while($row = mysqli_fetch_assoc($result))
    {
        $emparray[] = array($start=>$row);
        $start++;
     }
    
    打赏 评论

相关推荐 更多相似问题