Uncompressed data with Internet JOIN using PDO

Asked

Viewed 2,411 times

3

Hello I’m having problems when I’m returning a select made with some tables Inner Join as shown below:

inserir a descrição da imagem aqui

So I set up a Join Internet with my needs in my method:

public function tableUsuarioUM() {
    $consulta = PDOUtil::getStance()->prepare("SELECT pes.id_pessoa, pes.nome, pes.cpf, end.id_endereco, end.descricao, end.complemento, 
        end.cep,cid.id,cid.nome,cid.id_estado,est.id, est.nome, est.uf, est.id_pais FROM pessoa pes 
        INNER JOIN endereco end ON (pes.id_endereco = end.id_endereco)
        INNER JOIN cidade cid ON (end.id_endereco = cid.id)
        INNER JOIN estado est ON (cid.id = est.id)");
    $consulta->execute();
    while ($linha = $consulta->fetch(PDO::FETCH_OBJ)) { 
            echo '<thead>';
            echo '<tr>';
                echo '<td>'. $linha->nome. '</td>';
                echo '<td>'.$linha->cpf.'</td>';
                echo ' <td><a class="btn btn-primary" href="index.php?pagina=cadastrarUsuarios&id='.$linha->id_pessoa.'&nome='.$linha->nome.'&id_endereco='.$linha->id_endereco.'&cpf='.
                        $linha->cpf.'">Editar</a>';
                echo ' <a class="btn btn-danger" id="btn-apagar" href="index.php?pagina=../controller/controllerUser&id='
                .$linha->id_pessoa.'&nome='.$linha->nome.'&id_endereco='.$linha->id_endereco.'&acao=deletar">Deletar</a></td>';
            echo '</tr>';
            echo '</thead>';
    }
}

in time to bring the results he is bringing the name of the state instead of the name of the person, which may be happening.

inserir a descrição da imagem aqui

  • I expressed myself wrong, I will edit everything.

2 answers

3


In your query you have several fields with the same name in different tables, as php is not strongly typed it takes the last name and defines as the array key or object property

In the query below I left only the fields with the same name to highlight the situation:

SELECT pes.nome(1), cid.nome(2), est.nome(3) FROM pessoa pes 
        INNER JOIN endereco end ON (pes.id_endereco = end.id_endereco)
        INNER JOIN cidade cid ON (end.id_endereco = cid.id)
        INNER JOIN estado est ON (cid.id = est.id)

To get the right values in php you must set an alias for each field with the same name, this can be done with the word AS or simply set a new name after the column.

SELECT pes.nome AS p_nome, cid.nome c_nome, est.nome e_nome FROM pessoa pes 
        INNER JOIN endereco end ON (pes.id_endereco = end.id_endereco)
        INNER JOIN cidade cid ON (end.id_endereco = cid.id)
        INNER JOIN estado est ON (cid.id = est.id)
  • very good explanation.

  • I think I’m writing something wrong

  • after the p_name has no comma?

  • Yes, there was a comma missing, thanks for the remark.

  • "SELECT pes.id_pessoa, pes.nome AS p_nome, pes.cpf, end.id_endereco, end.descricao, end.complemento, &#xA; end.cep,cid.id,cid.nome c_nome,cid.id_estado,est.id, est.nome e_nome, est.uf, est.id_pais FROM pessoa pes &#xA; INNER JOIN endereco end ON (pes.id_addressee = end.id_addressee) INNER JOIN city Cid ON (end.id_addressee = Cid.id) INNER JOIN state est ON (Cid.id = est.id)" yet n funfou

  • no mysql funciona mas no php não: SELECT pes.id_pessoa, pes.nome as p_nome, pes.cpf, end.id_endereco, end.descricao, end.complemento, &#xA; end.cep, cid.id, cid.nome as c_nome, cid.id_estado, est.id, est.nome as e_nome, est.uf, est.id_pais FROM pessoa pes &#xA; INNER JOIN address end ON (pes.id_addressee = end.id_address) INNER JOIN city Cid ON (end.id_address = Cid.id) INNER JOIN status est ON (Cid.id = est.id);

  • solved the problem.

Show 2 more comments

1

As @rray demonstrated what it needed would be the aliases nicknamed for some columns of some tables that are equal so the method looked like this:

public function tableUsuario() {
    $consulta = PDOUtil::getStance()->prepare("SELECT pes.id_pessoa, pes.nome as p_nome, pes.cpf, end.id_endereco, end.descricao, end.complemento, 
        end.cep, cid.id, cid.nome as c_nome, cid.id_estado, est.id, est.nome as e_nome, est.uf, est.id_pais FROM pessoa pes 
        INNER JOIN endereco end ON (pes.id_endereco = end.id_endereco)
        INNER JOIN cidade cid ON (end.id_endereco = cid.id)
        INNER JOIN estado est ON (cid.id = est.id)");
    $consulta->execute();
    while ($linha = $consulta->fetch(PDO::FETCH_OBJ)) { 
            echo '<thead>';
            echo '<tr>';
                echo '<td>'. $linha->p_nome. '</td>';

                echo '<td>'.$linha->cpf.'</td>';
                echo ' <td><a class="btn btn-primary" href="index.php?pagina=cadastrarUsuarios&id='.$linha->id_pessoa.'&nome='.$linha->p_nome.'&id_endereco='.$linha->id_endereco.'&cpf='.
                        $linha->cpf.'">Editar</a>';
                echo ' <a class="btn btn-danger" id="btn-apagar" href="index.php?pagina=../controller/controllerUser&id='
                .$linha->id_pessoa.'&nome='.$linha->p_nome.'&id_endereco='.$linha->id_endereco.'&acao=deletar">Deletar</a></td>';
            echo '</tr>';
            echo '</thead>';
    }
}

all right.

Browser other questions tagged

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