How to replace data with php mysql EX:

-1

I have a table with all states with a STATE ID column, and one of the right state name. When the user registers goes the number of the state with the register of him, then I need to make a query for example if in the register of the user it is written that the id of the state is 10, there it inquires of which the name of the state this with the id number 10 and displays the name

I'm with the code so, I have not added the schema I mentioned above!

$mysql = new mysqli("localhost", "joao", "123", "final");
    $query = $mysql->query("SELECT * FROM c_clientes INNER JOIN c_clientes_enderecos ON c_clientes_enderecos.cliente_id = c_clientes.cliente_id;");
        while($row = $query->fetch_array()){
            echo
            "<tr>" . "<td>" . $row['cliente_id']. "</td>" .
            "<td>" . utf8_encode($row['cliente_nome']. " " . $row['cliente_sobrenome']). "</td>" .
            "<td>" . $row['cliente_nome_mae']. "</td>" .
            "<td>" . $row['cliente_email']. "</td>" .
            "<td>" . $row['cliente_telefone']. "</td>" .
            "<td>" . $row['cliente_celular']. "</td>" .
            "<td>" . $row['cliente_cpf']. "</td>" .
            "<td>" . $row['cliente_rg']. "</td>" .
            "<td>" . $row['cliente_nascimento_mae']. "</td>" .
            "<td>" . $row['cliente_status']. "</td>" .
            "<td>" . $row['cliente_data_emprego']. "</td>" .
            "<td>" . $row['usuario_id']. "</td>" .
            "<td>" . $row['cliente_mae_estado_id']. "</td>" .
            "<td>" . $row['regiao_id']. "</td>" .
            "<td>" . $row['cliente_habilitado_regiao']. "</td>" .
            "<td>" . $row['endereco_id']. "</td>" .
            "<td>" . $row['endereco_cep']. "</td>" .
            "<td>" . utf8_encode($row['endereco_rua']). "</td>" .
            "<td>" . $row['endereco_numero']. "</td>" .
            "<td>" . $row['endereco_complemento']. "</td>" .
            "<td>" . $row['cidade_id']. "</td>" . 
            "<td>" . $row['estado_id']. "</td>" . //Aqui vem o id quero 
            //transformar no nome!
            "<td>" . $row['c_clientes_enderecos']. "</td>" . "</tr>"

            ;
        }   
    
asked by anonymous 27.04.2018 / 17:56

1 answer

0

For this you have to make a join, it is not a substitution but to bring the data that belongs to that region, for example:

$sql = "SELECT u.id as usuarioID, u.nome, e.id as estadoID, e.uf from usuario as u 
inner join estado as e
on e.id = u.estadoId
where u.id = 1";

$pdo = new \PDO('mysql:host=local;dbname=teste', 'root', '');
$consulta = $pdo->prepare($sql);
$consulta->execute();

$dados = $consulta->fetchAll();

foreach($dados as $dado) {
   echo $dado;
}

I have not tested this code, but I'm sure it will work, I did it on hand here now. Please test, if you return some error tell me so I can fix. Remember that you should replace the connection and query data.

    
27.04.2018 / 18:12