0

i'm trying to get some json print. I have a simple format but i didn't figure it out.

But my result is like that. {"datas":{"title":"table"}}

    $sql = "SELECT * FROM mytable";
    $check = mysqli_fetch_array(mysqli_query($con,$sql));
    $array = array("table"=>$check["title"]);
    $result=json_encode(array('datas' => $array));
    echo($result);
    mysqli_close($con);

I need this is my format {"datas":[{"0":"table","title":"table"}]}

I guess i miss something.

  • 1
    use mysqli_fetch_assoc instead, need a mysqli version of https://stackoverflow.com/questions/11480129/mysql-fetch-row-vs-mysql-fetch-assoc-vs-mysql-fetch-array so it can be closed. Same applies anyway. – Lawrence Cherone Nov 27 '17 at 18:46
  • $array[] = $check; it works but i need to put some other data manually. –  Nov 27 '17 at 18:55

1 Answers1

1

You should use this:

    $sql = "SELECT * FROM mytable";
    $check = mysqli_fetch_assoc(mysqli_query($con,$sql));
    $array = array("table"=>$check["title"]);
    $result=json_encode(array('datas' => $array));
    echo($result);
    mysqli_close($con);
Andrii Pryimak
  • 797
  • 2
  • 10
  • 33