0
I have the following select (using PDO):
$usuariosListados = $pdo->prepare("SELECT user_1, user_2 FROM usuarios WHERE user_1 = '".$_SESSION["usuario"]."' AND estatus = 2");
$usuariosListados -> execute();
$fAll = $usuariosListados -> fetchAll(PDO::FETCH_ASSOC);
$rCount= $usuariosListados -> rowCount();
// E o seguinte foreach
if(rCount) {
foreach($fAll as $itemUser) {
echo "Usuário amigo: ".$itemUser["user_2"];
}
} else {
echo "Nenhum registro encontrado.";
}
However, I would need to make another select, but in the following way:
$usuariosListados_2 = $pdo->prepare("SELECT user_1, user_2 FROM usuarios WHERE user_2 = '".$_SESSION["usuario"]."' AND estatus = 2");
And the foreach would be:
foreach($fAll_2 as $itemUser_2) {
echo "Usuário amigo: ".$itemUser["user_1"];
}
I wonder if there is any way to "join" these selects thus avoiding making 2 queries to the database. Remembering that, the user_1
and the user_2
will never be equal, and I need the value that corresponds to the "opposite" of the logged in user (which is the $_SESSION["usuario"]
).
Can you explain it better? It got a little confusing.
– Igor
Maybe I didn’t understand your final wish -- this consists only of giving the two users at once (just a query to the database). After that, I don’t think I understood its purpose - but I hope I was able to answer the question :|
– brazilianldsjaguar
(from what I understood and researched) the
IN
serves to return results that are equal toWHERE
, but I need the different. That is, ifuser_1
for = a$_SESSION["usuario"]
then in the foreach I will use theuser_2
and vice versa. There are ways to do this?– Igor
I was able to adapt your answer to my problem by just doing an if/Else check, thank you very much!
– Igor
I’m glad it worked out!
– brazilianldsjaguar