How do I put a select within the while?

2

Use Sublime text, Mysql Workbench and Xampp.

I made an inner join in the select below, in a table called Organizacoes.Copia. In this table there is a field called "id" and I need it to appear in this Php code.

I saw several videos and examples, and I could not do it. How do I put it to pull the copy of those returning clients in my variable " $result "? How do I include this in the while?

Here's the code ready:

<?php

$servidor = '****';
$user = '****';
$senha = '****';
$banco = 'Organizacoes';

// Cria conexão
$conn = new mysqli($servidor, $user, $senha, $banco);
// Checa conexão
if ($conn->connect_error) {
    die("Conexão falha: " . $conn->connect_error);
} 

$sql = "SELECT   Org.id
        ,Org.nome 
        FROM Organizacoes as Org 

        INNER JOIN Organizacoes.Rel_Organizacoes as Grupo on Org.id = Grupo.id_Organizacao
        INNER JOIN Organizacoes.Copias as Copia on Org.id = Copia.id_Organizacao

        WHERE  Grupo.id_grupo = 210 
        GROUP BY Org.id
        ORDER BY Org.id";

$result = $conn->query($sql);

echo " <tr>";


if ($result->num_rows > 0) {
    // dados de saída de cada linha
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - nome: " . $row["nome"]. " " . "<br>";
    }

} else {
    echo "0 results";
}
$conn->close();

?>
    
asked by anonymous 07.05.2018 / 14:22

2 answers

1

Good morning, you could use this solution. I do not know if php would be correct, but in the query it could be that solution.

<?php

$servidor = '****';
$user = '****';
$senha = '****';
$banco = 'Organizacoes';

// Cria conexão
$conn = new mysqli($servidor, $user, $senha, $banco);
// Checa conexão
if ($conn->connect_error) {
    die("Conexão falha: " . $conn->connect_error);
} 

$sql = "SELECT Copia.*,  Org.id
    ,Org.nome 
    FROM Organizacoes as Org 

    INNER JOIN Organizacoes.Rel_Organizacoes as Grupo on Org.id = Grupo.id_Organizacao
    INNER JOIN Organizacoes.Copias as Copia on Org.id = Copia.id_Organizacao

    WHERE  Grupo.id_grupo = 210 
    GROUP BY Org.id
    ORDER BY Org.id";

$result = $conn->query($sql);

echo " <tr>";


if ($result->num_rows > 0) {
    // dados de saída de cada linha
    while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"].  "id_Copia: " . $row["Copia.id"]. " - nome: " . $row["nome"]. " " . "<br>";
    }

} else {
    echo "0 results";
}
$conn->close();

?>
    
07.05.2018 / 15:06
0

You can do this

$sql="
    SELECT
        Copia.id AS copia_id,
        Org.id,
        Org.nome 
        FROM Organizacoes as Org 

        INNER JOIN Organizacoes.Rel_Organizacoes as Grupo on Org.id = Grupo.id_Organizacao
        INNER JOIN Organizacoes.Copias as Copia on Org.id = Copia.id_Organizacao

        WHERE  Grupo.id_grupo = 210 
        GROUP BY Org.id
        ORDER BY Org.id";

$result = $conn->query($sql);

echo " <tr>";


if ($result->num_rows > 0) {
    // dados de saída de cada linha
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " copia-id: " . $row["copia_id"]. " - nome: " . $row["nome"]. " " . "<br>";
    }

} else {
    echo "0 results";
}
    
07.05.2018 / 15:15