F
F
forwox2020-10-09 12:16:47
PHP
forwox, 2020-10-09 12:16:47

How to generate JSON from SQL query using PHP?

Good day!
Please help me solve the problem.

There is a mysql table:
id | parentid | name | value
-----------------------
1 | 0 | name | Vasya
2 | 1 | date |
3 | 2 | min | 12/09/2020
4 | 2 | max | 12/12/2020
...........

How can I display the contents of this table in json format on the page using php ??

{
"name": "Вася",
  "date": {
     "min": "12/09/2020",
     "max": "12/12/2020"
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2020-10-09
@Adamos

Your json does not match your data in the database.
date must be a child of name, which also creates a conflict with value.
"There are things in the world so crooked"...
Obviously, the database should simply have 0, not 1.
You read all the lines, sort through, add to the object depending on the parent. Ready-made code is not accepted here, this is not freelancing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question