Answer the question
In order to leave comments, you need to log in
How to display data from the second table as an object?
There are two tables - product and type . The product
table has a foreign key on the typeID field of the type table . With this query, I join two tables into one result:
SELECT id, `product`.title, `product`.typeID, `type`.title FROM `product` LEFT JOIN type on `product`.typeID=`type`.typeID where `product`.typeID=1
json_encode()
I output json. How can I display the result through php so that the typeID and title values of the type table are like an object in json. [
{
"id":"1",
"title":"Water",
"type": {
"typeID":"3",
"title":"liquid"
}
}
]
Answer the question
In order to leave comments, you need to log in
if you dream that a ready-made array will magically return from the request in this form, then nothing. Queries return one-dimensional arrays.
You can choose like this:
SELECT id, `product`.title, `product`.typeID, `type`.title,
type`.typeID type_typeID, type`.title type_ttitle
FROM `product`
LEFT JOIN `type`
on `product`.typeID=`type`.typeID
where `product`.typeID=1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question