How do I put a select inside the while?

Asked

Viewed 367 times

2

Sublime text, Mysql Workbench and Xampp usage.

I made a Join Inner in select below, in a table called Organizations.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 could not do. How I put to pull the copy of these returning customers in my variable "$result"? How do I include that in while?

Follow 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();

?>
  • query brings the results?

  • 1

    Bring, it brings the client name and his code.. This "copy" is an id, but from another table. But I can’t include this "Id.Copy in this current while.

  • is because you didn’t ask to select him

  • 1

    So I actually took it to post here because I can’t ride in while.. If I put it in my $result result select Org.id ,Org.name , Copy.id .. How do I put it in while? How do I structure? This part that I’m having difficulty knows?

  • 1

    I will be very grateful.. I realized that it is the doubt of many beginners in php.

  • I accidentally deleted my mouse at the end of my life

  • calls the id you want and gives a var_dump($result) and look at the structure

  • 1

    Then I add the id.copy to my $result variable, and put the "var_dump ($result) " after while? Where do I leave it?

  • 1

    Could I put a "query2" with "$result2" inside that while? Or do you think it’s better to do it the way you said?

  • may be before while, provided it is dps that $result appeared

  • The var_dump worked?

  • 1

    It went in pieces.. He pulled the copy, but replaced my other account id..

  • then the problem is in the nomenclature of the fields of Tablas, this with equal names, just change

  • so you can separate within $Row['id_copy'] from $Row['id']

  • 1

    But how do I do it? If I change the name it gives error: Notice: Undefined index: ['id_copia'] in C: xampp htdocs query.php on line 29

  • 1

    The name of the fields are equal, but the tables are different. Taba.id, Tabb.id.. Understood?

  • Post the var_dump return

Show 12 more comments

2 answers

1

Good morning, you could use this solution. I don’t know if php would be correct, but in the query can be this 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();

?>
  • So in mysql the query even works.. But it would pull all the data from the copy table, and I just needed the id. In php, it gives error: Notice: Undefined index: Copia.id in C: xampp htdocs query.php on line 39 id: 1044 id_Copia: - name: EXAMPLE CLIENT 1

0

You can do it like 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";
}
  • 1

    Certified Wess Smith.. Result: id: 110 copy-id: 8451 - name: CLIENT 2. I can only ask one last question?

  • of course, when you want

  • 1

    This id_copy, I need the latest id. Can you tell me any function I can use? I even tried max(id_copia), but he respects only for some..

  • ORDER BY Org.id, Copy.id DESC

  • Respect in some only.. When you directed me to use the var_dump he pulled straight

  • as well as in some?

  • For example, I have Client 5.. This client has two copy.id. (One from 2011 and the other from 2018). It shows Client 5 - And the copy from 2011. Hence has the Client 2 which has two copies tbm (2015 and 2017). Dai it shows: Client 2 - Copy 2017 (correct, pq is the last).. Got it?

  • understood yes, but in the table the id of 2011 ta smaller or bigger q 2018?

  • confirms, because sometimes that’s Bozinho

  • So the id gives 2011 is 1195.. And the id of 2018 is 1660. The two linked to the Client 5.

  • tries to place an ORDER BY Org.id, Copy.id DESC LIMIT 1

  • With Limit 1 he’ll only pull a single customer.. I put on Where to be the "Client 5" that is with this problem and he keeps pulling the copy of 2011. Strange he does not respect right..

  • yes, it’s weird, because as you put DESC it would already automatically catch the last

  • It is. I have tried and I am trying several ways and it does not work.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.