¿Por qué $ friend no se expande en la consulta FQL JSON?

I'm trying to setup a multiquery in FQL in PHP, with a variable. But I can't seem to get it to work correctly

  $fql = '{
        "uid"   : "SELECT uid FROM user WHERE username = \"$friend\"",
        "album": "SELECT object_id, link, aid, cover_pid, location, name FROM album WHERE owner IN (SELECT uid FROM #uid)",
        "photo": "SELECT src FROM photo WHERE pid IN (SELECT cover_pid FROM #album)"
    }';

This is basically trying to get the cover photos of your friends album.

If I put in a friend's username directly in place of $friend, I get all the results, but not with the above code where $friend is dynamically set.

What went wrong!!?? Thanks

preguntado el 10 de marzo de 12 a las 04:03

Note that the outside quotes are 's y no "s. -

4 Respuestas

Strings using single quotes are not evaluated by PHP. In this case you should concatenate them using the dot operator:

$fql = '{
        "uid"   : "SELECT uid FROM user WHERE username = \"' . $friend . '\"",
        "album": "SELECT object_id, link, aid, cover_pid, location, name FROM album WHERE owner IN (SELECT uid FROM #uid)",
        "photo": "SELECT src FROM photo WHERE pid IN (SELECT cover_pid FROM #album)"
    }';

Deberías echar un vistazo a the "PHP bible".

respondido 10 mar '12, 04:03

In PHP, variables inside of double quoted strings will be evaluated, while variables inside of single quoted strings will not.

Your best solution is to reverse the quotes so your string looks like this:

$fql = "{
    'uid'   : 'SELECT uid FROM user WHERE username = '$friend',
    'album': 'SELECT object_id, link, aid, cover_pid, location, name FROM album WHERE owner IN (SELECT uid FROM #uid)',
    'photo': 'SELECT src FROM photo WHERE pid IN (SELECT cover_pid FROM #album)'
}";

Esto causará $friend to be evaluated to the value of your variable.

respondido 10 mar '12, 05:03

This isn't quite valid. The generated JSON is bonkered around '$friend' due to unbalanced quotes and "clobbered" quotes. Likely just want to suck it up and use \" or a here-doc. - user166390

$text = "85";
print "The number is: ".$text.".";

esto dará como resultado:

The number is: 85.

respondido 10 mar '12, 04:03

In SQL usually all varchar are between single quotes instead of double Have you tried this?

"SELECT uid FROM user WHERE username = '" . $friend . "'",

respondido 10 mar '12, 04:03

This still won't work. The entire JSON is a string (and single quoted in this case). - user166390

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