Get messages sent and received from MySQL

0

I'm developing a personal messaging system and I need to list the messages I've received and the messages I've sent, but I'm having difficulties, because the code I've just got can pick up all that I've sent or all that I've received and never both. The code I'm using

<?php    
$query = mysql_query("SELECT pr.id_perfil, pr.id_perfil_enviou, pr.recado, pe.nome as NomeEnviou, prm.nome as NomeRecebeu FROM perfil_recados AS pr INNER JOIN perfil AS pe ON pe.id = pr.id_perfil_enviou INNER JOIN perfil AS prm ON prm.id=pr.id_perfil WHERE (pr.id_perfil='961' AND pr.id_perfil_enviou = '15') OR (pr.id_perfil='15' AND pr.id_perfil_enviou = '961')");

while($exe = mysql_fetch_array($query)){                    
   echo 'De: '.$exe['NomeEnviou'].' para '.$exe['NomeRecebeu'];    
}
?>
    
asked by anonymous 22.09.2014 / 19:48

2 answers

1

Consider using the UNION clause of SQL.

  

In MySQL the UNION keyword is used to combine the result of   multiple SELECT statements in a single result.

Example:

example:

(SELECT * FROM clientes WHERE id_cidade = 1 LIMIT 3)
UNION (SELECT * FROM clientes WHERE id_cidade = 5 LIMIT 3)
UNION (SELECT * FROM clientes WHERE id_cidade = 8 LIMIT 3)

Reference: link

    
23.09.2014 / 17:23
0

And if you do so

SELECT pr.id_perfil, 
       pr.id_perfil_enviou, 
       pr.recado, 
       pe.nome AS NomeEnviou, 
       prm.nome AS NomeRecebeu
FROM perfil_recados AS pr 
INNER JOIN perfil AS pe ON pe.id = pr.id_perfil_enviou 
INNER JOIN perfil AS prm ON prm.id = pr.id_perfil 
WHERE pr.id_perfil IN (15, 961) 
   OR pr.id_perfil_enviou = (15, 961);
    
23.09.2014 / 17:38