How to replace data with php mysql EX:

Asked

Viewed 124 times

-1

I have a table with all the states with a column of STATE ID, and one of the name of the right state. When the registered user goes the state number with his registration, there need to make a query for example if in the user’s registration written that the state id is 10, there is the query of which the state name is with the id number 10 and displays the name

I have the code like this, I haven’t added the schematic I quoted above yet!

$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>"

            ;
        }   
  • Clarify your question, related to php? Or the query in the database? Include your code.

  • I do not have a code, but it is to replace the ID by the name in the query dai

  • And what is the database, name and structure of the tables?

  • I have ID 1 for ex, and in the client’s register ta state 1, how do I replace by name

  • Structure http://prntscr.com/jawhn2

  • If the state name is in another table, you have to do a JOIN

  • http://prntscr.com/jawi9m

  • Are already connected

  • 1

    Possible duplicate of How to do Inner Join in mysql?

  • See the above question you should get.

  • but I need to replace the number by the name

  • blz I’ll try! anything notice here if it worked

Show 7 more comments

1 answer

1


For this you have to make a Join, it is not a substitution but bring the data that belongs to that determined region, 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 didn’t test that code, but I’m sure it’ll work, I did it on hand here now. Please test, if you return any error tell me so I can fix it. Remembering that you must replace the connection data and the queries.

Browser other questions tagged

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