Phantom001
Phantom001

Reputation: 57

PHP build a specific JSON string from postgresql Database

I want use zachhunters json to html tabel script with my postressql db so i need a specific JSON string format but i dont know how to build it ... (i am new in json)

Link to the Script

I get my data using

$result = pg_fetch_array($rs, NULL, PGSQL_ASSOC);

When i use json_encode($result) i get this:

{"id":"2","surname":"Max","name":"Muster"} etc.

For the Script i need somthing like:

{ "d": "[{\"id\":1,\"Username\":\"Sam Smith\"},{\"id\":2,\"UserName\":\"Fred Frankly\"}]" }

How can i handle this?

Upvotes: 2

Views: 1599

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318518

In each loop where you fetch an element from your database store the $result in an array $rows.

After the loop simply use json_encode(array('d' => $rows)) to create JSON containing all your data.

Upvotes: 2

Related Questions