0

I'm facing a big problem in php while building a json object array. I'm trying to fetch records from mysql and then put them in the below format but it doesn't work.

This is how i want :

{ "records":[ {"id": 1,"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"}, {"id": 2,"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"}, {"id": 3,"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"}, {"id": 4,"Name":"Around the Horn","City":"London","Country":"UK"}, {"Name":"B's Beverages","City":"London","Country":"UK"}, {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"}, {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"}, {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"}, {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"}, {"Name":"Bon app'","City":"Marseille","Country":"France"}, {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"}, {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"}, {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"}, {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}, {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"} ] }

what i get

["records",{"name":"abc","email":"[email protected]"},{"name":"def","email":"[email protected]"},{"name":"ghi","email":"[email protected]"}]

notice the colon (:) after the "records".

My PHP CODE :

$rows[] = "records";


$a = mysql_query("", $connect);
while ($b = (mysql_fetch_array($a)) ){
extract($b);
$rows[] = array('name' => "$accountnumber", 'email' => "$email"); 
}

echo json_encode($rows);
7
  • Why dont you use json_encode() function as long as you can retrieve data as array from database? Commented Jan 5, 2019 at 13:41
  • @NigelRen please check now..i've added my code Commented Jan 5, 2019 at 13:42
  • use the flag JSON_FORCE_OBJECT in json_encode Commented Jan 5, 2019 at 13:43
  • Your expected data and the data you get is different Commented Jan 5, 2019 at 13:44
  • 1
    As a general note - you should start to move away from the old mysql_ api - stackoverflow.com/questions/12859942/… Commented Jan 5, 2019 at 13:49

1 Answer 1

3

You are adding the records level in the wrong place, you are just adding it as an element to the array. This adds each row as a child of this element ($rows["records"][])...

$rows= [];

$a = mysql_query("", $connect);
while ($b = (mysql_fetch_array($a)) ){
    $rows["records"][] = array('name' => $b["accountnumber"], 'email' => $b["email"]);
}

echo json_encode($rows);

Also - avoid using extract() if possible and add the values from the array you fetch instead.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a genius!! Thanks Nigel Ren, your answer helped me to build a JSON response exactly how i wanted it.

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.