Postgresql changes letter with accent

Asked

Viewed 1,172 times

1

Look I’m saving data on SGBD Postgresql, only that the data (NAME, CITY, STREET), I am saving in capital to facilitate when searching.

He’s saving normal, the problem is that when I save a word in capital letters and accent, the Postgresql turns the letter into a lower case.

  $nome = strtoupper($cliente->getNome());
        $cpf = $cliente->getCpf();
        $endereco = $cliente->getEndereco();
        $cidade = strtoupper($endereco->getCidade());
        $rua = strtoupper($endereco->getRua());
        $numero = $endereco->getNumero();
        $telefones = $cliente->getTelefones();
        $telefone1 = $telefones[0];
        $telefone2 = $telefones[1];

      $valorArmazenar = $this->conexao->prepare("INSERT INTO cliente(nome_cliente,cidade, rua, numero, telefone1, telefone2, cpf) VALUES(:nome,:cidade,:rua,:numero,:telefone1,:telefone2, :cpf)");

      $valorArmazenar->bindValue(":nome", $nome);
      $valorArmazenar->bindValue(":cidade", $cidade);
      $valorArmazenar->bindValue(":rua", $rua);
      $valorArmazenar->bindValue(":numero",$numero);
      $valorArmazenar->bindValue(":telefone1", $telefone1);
      $valorArmazenar->bindValue(":telefone2", $telefone2);
      $valorArmazenar->bindValue(":cpf", $cpf);
      $valorArmazenar->execute();

See that the "A" of the word JOHN he left John "A" in tiny, and this makes it difficult at the time of my search by the record. inserir a descrição da imagem aqui

  • What is the CHARSET of the table?

  • @Rodrigosartorijarouche the charset is utf-8

  • I believe that $valorArmazenar->bindValue(":nome", mb_substr($nome)); solves the problem ;)

  • 1

    Rodrigo, Edson’s answer below will help you. Regardless of this, I suggest reading this article: (Don’t mind why the title seems offensive - the name is very important) http://local.joelonsoftware.com/wiki/O_M%C3%Adnimo_absoluto_que_todos_programmeres_de_software_they need,_Absolutely,Positivamente_de_Saber_Sobre_Unicode_e_Conjuntos_de_Caracteres(Apologies!)

2 answers

3


This is because strtoupper failed to turn the accented letter to uppercase, in this case use the mb_strtoupper in place.

$nome      = mb_strtoupper($cliente->getNome(), 'UTF-8');
$cpf       = $cliente->getCpf();
$endereco  = $cliente->getEndereco();
$cidade    = mb_strtoupper($endereco->getCidade(), 'UTF-8');
$rua       = mb_strtoupper($endereco->getRua(), 'UTF-8');
$numero    = $endereco->getNumero();
$telefones = $cliente->getTelefones();
$telefone1 = $telefones[0];
$telefone2 = $telefones[1];

Clicking here to understand more about the case.

Tip

Whenever possible create your database in ENCODING UTF8, LC_COLLATE en_US.UTF-8 and LC_CTYPE en_US.UTF-8, otherwise you may have this type of problem and others like sort an alphabetic query and it brings first those that start with lowercase letters and then those that start with uppercase letters (or the opposite, I don’t remember).

-1

If your Postgresql has LATIN1 chartset support, just recreate the database with the following features:

    CREATE DATABASE nome_do_banco ENCODING 'LATIN1' LC_COLLATE 'pt_BR.ISO-8859-1' LC_CTYPE 'pt_BR.ISO-8859-1' template template0;

Take the test again and see how it turned out.

  • Do not recommend this before knowing how the subject set up the development environment. Change the charset of the database reflects in chain: web server, php, coding of scripts, etc.

  • it is true, I created a standard postgresql database

  • 1

    You don’t solve a problem with international characters kicking encodings to use. You have to understand what’s going on. In particular, the use of LATIN1 will only complicate if there are characters that are not defined in this encoding. Moreover, the problem in this case is clearly the PHP side, not the Postgres side

Browser other questions tagged

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