Question: Hi I am new in php and i dont know how can i do this?

I have four rows in my data base and i want to encode a Json array of these rows using php how can i do this.

below is my code please look into this and give me a suggestion--

```
<html>
<head>
<title>First</title>
<body>
<?php
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ContactDB", $con);

$result = mysql_query("SELECT * FROM mycontacts");

echo "<table border='1'>
<tr>
<th>PhoneNumber</th>
<th>Name</th>
<th>Lastname</th>
</tr>";

 while($row = mysql_fetch_array($result))
  {
  $data=array("PhoneNumber"=>$row['PhoneNumber'],"Name"=>$row['Name']); 
print_r (json_encode(array_chunk($data, 1, true)));  
}

mysql_close($con);
?>

</body>
</head>
</html>

```

I am getting response like this

[{"PhoneNumber":"1234567"},{"Name":"Ujjwal"}][{"PhoneNumber":"765423"},{"Name":"ABC"}][{"PhoneNumber":"098765123"},{"Name":"A"}]

but i want it in this format

[{"PhoneNumber":"1234567", "Name":"X"},{"PhoneNumber":"765423", "Name":"ABC"},{"PhoneNumber":"098765123", "Name":"A"}]

Answer: 
Replace

```
while($row = mysql_fetch_array($result))
{
  $data=array("PhoneNumber"=>$row['PhoneNumber'],"Name"=>$row['Name']); 
  print_r (json_encode(array_chunk($data, 1, true)));  
}

```

with 

```
$data = array();
while($row = mysql_fetch_array($result))
{
  $data[] = array("PhoneNumber"=>$row['PhoneNumber'],"Name"=>$row['Name']); 
}
print_r (json_encode($data));  

```
Try this:

```
$json = json_encode($data)

```