2

I am new in PHP and just trying to encode mysql table's data into JSON format, and to get that i am using below script:

<?php
    $objConnect = mysql_connect("localhost","root","root");
    $objDB = mysql_select_db("mydatabase");
    $strSQL = "SELECT * FROM member WHERE 1  ";
    $objQuery = mysql_query($strSQL);
    $intNumField = mysql_num_fields($objQuery);
    $resultArray = array();
    while($obResult = mysql_fetch_array($objQuery))
    {
        $arrCol = array();
        for($i=0;$i<$intNumField;$i++)
        {
            $arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
        }
        array_push($resultArray,$arrCol);
    }

    mysql_close($objConnect);

    echo json_encode($resultArray);
?>

and getting result in below format:

    [
    {
      "MemberID":"1",
      "Name":"Weerachai"
    },
    {
      "MemberID":"2",
      "Name":"Win"
    }
]

But what if i want to get response like this:

{
  "members": [ // i want to add this array
    {
      "MemberID":"1",
      "Name":"Weerachai"
    },
    {
      "MemberID":"2",
      "Name":"Win"
    }
  ]
}

1 Answer 1

2

Just create an array like the one you want and encode that.

echo json_encode(array('members' => $resultArray));
Sign up to request clarification or add additional context in comments.

1 Comment

sorry thank you i made a stupid mistake, i ticked your answer as useful and will accept after two minutes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.