Accentuation problem when retrieving records in SQL Server

3

When I retrieve records from a SQL database Serve with PHP errors appear in the accent.

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

Collation : Latin1_General_CI_AS

Bank connection file:

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

Bank Fields:

PHPqueryresult

    
asked by anonymous 14.07.2015 / 14:15

6 answers

9

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

utf8_encode($variavel_contendo_string)

And the problem has been solved!

This function encodes the string for utf-8.

    
29.07.2015 / 21:21
3

Try to use an equal collation across your entire application.

Since you can not change the Bank, configure 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 setting up PHP and Database, you need to change the charset of your pages to work with ISO-8859-1 instead of UTF-8.

    
14.07.2015 / 15:08
1

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

    
14.07.2015 / 14:21
1

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

$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");
    
14.07.2015 / 14:57
0

1st Alternative:

Open the file .php in notepad ++ and go to the formatar menu button 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 you are rendering is not in the utf-8 standard, so add this line to it:

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

3rd Alternative:

Add this to the beginning of your .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 be as below:

default_charset = "UTF-8"

6th Alternative:

If you do not have access to the file php.ini add the following line at the top of the document .php :

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

7th Alternative:

ini_set('mssql.charset', 'UTF-8');
    
14.07.2015 / 14:23
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 anyone who wants to keep their application in UTF-8 and use a database in different coding .

Convert the data to fetch using overhead, 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);
    }
}
    
14.07.2015 / 19:01