Accentuation problem while recovering records in SQL Server

Asked

Viewed 17,792 times

4

When I recover records from a SQL Serve database with PHP errors appear in accentuation.

But in the database, the accents are correct. Is there any way to handle this with PHP? I don’t have permissions to change the collation bank.

Collation of the bank: Latin1_general_ci_as

File connection to the bank:

$host='xxxxxx';
$user='xxxxx';
$database='xxxxx';
$pass='xxxxx';

$con=mssql_connect($host, $user, $pass) or die ("Erro de conexão com o banco de dados"); 
mssql_select_db("$database") or die ("Erro ao selecionar banco de dados");

Fields on the bench:

Campos no banco

Result of PHP query

Resultado da consulta

  • Ever tried to use escape ?

  • No. How do you use it? I don’t know it

  • But if the accents are correct in the bank then it is not necessary to change the collation

  • Edit your question with the parts that are in trouble @Amandaoliveiradelima

  • What Collation and Field Type (nvarchar) ?

  • @gmsantos, I don’t have access to the bank structure to see the field type. But the Collation is Latin1_general_ci_as

  • Amanda, click [Edit] and enter the important comments in the question body (collation). Also provide how you are connecting PHP to SQL Server (it is by PDO?)

  • @gmsantos, I edited there

  • Place header('Content-Type: text/html; charset=ISO8859-1'); resolve?

  • Aeeeee @gmsantos this worked, thank you very much!!

  • @Amandaoliveiradelima converted the comment to a response. If you solved your problem, you can mark it as correct. To understand how things work around here start by doing a [tour]

Show 6 more comments

6 answers

13


I found the solution to this problem in a very simple way. When printing the string, I used the function before

utf8_encode($variavel_contendo_string)

and the problem was solved!

This function encodes the string to utf-8.

3

Try to use the same Collation throughout your application.

Since you cannot change the Database, set up PHP to work with Latin-1 (ISO-8859-1):

header('Content-Type: text/html; charset=iso-8859-1');

Or in the file php.ini

default_charset = "iso-8859-1"

In addition to PHP and Database configuration, you need to change the charset of your pages to work with ISO-8859-1 instead of UTF-8.

  • It solved the problem of accentuating the bank’s fields, but it messed up the accentuation of the rest of the page. I changed the UTF-8 header to <meta http-equiv="Content-Type" content="text/html;charset=ISO8859-1"> and it doesn’t work

  • Amanda, try to check the file charset .html or .php. It is in uft-8?

  • No, I changed all the charset to iso-8859-1

1

You can use the PHP function:html_entity_decode($valor);

  • In my opinion this is the best answer.

1

You can use sqlsrv_connect in which the parameter exists CharacterSet UTF-8.

$host="xxxxxx";
$user="xxxxx";
$database="xxxxx";
$pass="xxxxx";

$connArr = array(
    "Database"     => $databae,
    "UID"          => $user,
    "PWD"          => $pass,
    "CharacterSet" => "UTF-8"
);

$conn = sqlsrv_connect($host, $connArr) or die("Erro de conexão com o banco de dados");
  • That function didn’t work. Gave the following error: Fatal error: Call to Undefined Function sqlsrv_connect() in /var/www/html/financial/core/includes/conect.php on line 17

0

1st Alternative:

Open the file .php on Notepad++ and go to menu button formatar and select the option: Codificação em UTF-8 (sem BOM), it is possible that the PHP source file is not utf-8.

2nd Alternative:

The page .html which is rendering not in the standard utf-8, so add this line to it:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

3rd Alternative:

Add this at the beginning of your file .php

header('Content-Type: text/html; charset=utf-8');

4th Alternative:

Add the following line to the file .htaccess

AddDefaultCharset UTF-8

5th alternative:

Edit the following line in the file php.ini to stand as below:

default_charset = "UTF-8"

6th Alternative:

If you don’t have access to the file php.ini add the next line at the top of the document .php:

ini_set('default_charset', 'UTF-8');

7th Alternative:

ini_set('mssql.charset', 'UTF-8');
  • Not always UFT-8 will work, depends on the collation of the bank.

  • None of these alternatives worked :/

  • The accents are really perfect on the bench? which the collation of the bank?

  • Yes @Ricardo, the accents are perfect in the bank, only gives problem when I try to display in php. The Collation is Latin1_general_ci_as

  • @Amandaoliveiradelima I added new alternatives.

  • @Icardo, none ran yet rs

  • Usually this error occurs because of file format in case page that was saved wrong. Unfortunately for your case it didn’t work. But most of the time with the above modifications it works.

Show 2 more comments

0

There are answers that already make a good instruction on how to model your application for the same encoding of the database.

However, I want to leave my answer to be used by those who want to keep their application in UTF-8and use a database in different coding.

Convert the data in time to fetch using overload, as in this example:

class DrivePDOUtf8 extends DrivePDO
{
    protected function toUtf8($dados)
    {
        if (is_array($dados) || is_object($dados)) {
            foreach ($dados as $i => $r) {
                if (is_array($r) || is_object($r)) {
                    $dados[$i] = $this->toUtf8($r);
                } else {
                    $dados[$i] = utf8_encode($r);
                }
            }
        } else {
            $dados = utf8_encode($dados);
        }

        return $dados;
    }

    public function fetch($sql, $mode = null, $all = false)
    {
        $fetch = parent::fetch($sql, $mode, $all);

        return  $this->toUtf8($fetch);
    }
}

Browser other questions tagged

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