Procesar los resultados de SQL JOIN con PHP

I have an issue with processing data with PHP I retrieved from MySQL. This is the table schema:

id | firstname | lastname

id | firstname | lastname

child_id | parent_id

I need to save the data this way, because I'd like to link multiple parents to one child. Now when I want to retrieve a child, I'd like to get all the information of the child, but also the parent id's. This obviously is a JOIN query, so I use:

FROM children c
JOIN parent_child_link l on

Now, when this child with id=1 has 2 parents (lets say with id's 1 and 2), I get this as a result:

id | firstname | lastname | parent_id
1    test        test       1
1    test        test       2

If I process this in PHP, I would get 2 arrays, but I want this in one array, e.g.

    'firstname' => 'test', 
    'lastname' => test',
    'parents' => array(1, 2)

¿Cómo puedo conseguir esto?

¡Muchas gracias!

preguntado el 03 de mayo de 12 a las 21:05

2 Respuestas

You can use GROUP_CONCAT to return a comma seperated array of the values

contestado el 03 de mayo de 12 a las 21:05

you could process you query result in php. create a new array indexed on the children id and for each item in the query result, add it to the array if it's not there already. If it is, add the parent id to its "parents" item.

contestado el 03 de mayo de 12 a las 21:05

I think that will cause more work. Now I just need to execute the query with GROUP_CONCAT and explode the parent id's. - ESTEGE

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.