0

I have a php script that returns a resultset from mysql database like that:

nombre_musico apellidos_musico

  • Rafael Ruda
  • Antonio Manuel Rios
  • Alejandro Astola

The source code to concat string of musicians:

while($fila = mysqli_fetch_object($result)){
            $apellido = str_replace("\r","",$fila->apellidos_musico);;
            $nombre = str_replace("\r","",$fila->nombre_musico);
            $musicos .= $nombre . " " . $apellido .",";
        }

the sql query is:

    $sql = "select gr.nombre_grupo, gr.anyo_creacion, gr.descripcion, ms.nombre_musico, ms.apellidos_musico\n"
    . "from musico ms, canta_para cp, grupo gr\n"
    . "where ms.id_musico = cp.musico_id and gr.id_grupo = cp.grupo_id and gr.nombre_grupo = '".$name_group."'";

and the code to execute the query is:

$result = mysqli_query($mysqli, $sql);

I want to obtain result like this:

"musicos":"Rafael Ruda,Antonio Manuel Rios,Alejandro Astola"

but the result I obtain is:

"Rafael Ruda,Antonio Manuel Rios,"

What I must do to obtain te correct concatenated string. I wonder for a little help as soon as possible.

4
  • what field are those names contained in? Commented Aug 1, 2014 at 20:56
  • ms.nombre_musico, ms.apellidos_musico in the select Commented Aug 1, 2014 at 20:58
  • 2
    Is the problem that you only get two names? Or is the problem that you want to see "musicos" : "..." Commented Aug 1, 2014 at 20:59
  • the problem is that i got two names instead of three the "musicos":"..." is a json property that returns the php script Commented Aug 1, 2014 at 22:16

1 Answer 1

1

this will return exactly the format you entered:

SELECT CONCAT('\"musicos\":',gr.nombre_grupo,', ',gr.anyo_creacion,', ',gr.descripcion,', ',ms.nombre_musico,', ',ms.apellidos_musico)
Sign up to request clarification or add additional context in comments.

1 Comment

That is not what i want. I want to concat the three names and assign it to an json object property which name is "musicos"

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.