Charset Mysql and PHP

3

My table in mysql has UTF-8 formatting and all accents appear correctly, but when I make the request with PHP and display the data they do not appear in UTF-8 format.

p.s: My page already has the goal for utf-8.

Do you know what might be causing this problem?

    
asked by anonymous 05.08.2015 / 16:25

1 answer

3

How is your connection made, Mysql Conect, Mysqli, PDO?

I think this would be the solution:

@mysql_query("SET NAME 'utf8'");
@mysql_query("SET character_set_connection=utf8");
@mysql_query("SET character_set_client=utf8");
@mysql_query("SET character_set_results=utf8");

PDO:

$db = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf-8", $dbUser, $dbPass);
$db-> exec("SET CHARACTER SET utf8");

Mysqli:

$con=mysqli_connect("localhost","user","pass","db");
// Verifica conexão
if (mysqli_connect_errno())
  {
  echo "Falha ao fazer conexão: " . mysqli_connect_error();
  }

// Set utf8
mysqli_set_charset($con,"utf8");

mysqli_close($con);
?>

CODIGNITER:

Please check if config.php and database.php are like this

config.php

'$config['charset'] = 'UTF-8'

database.php:

$db['default']['char_set'] = 'utf8'; 

and

$db['default']['dbcollat'] = 'utf8_general_ci';

UFT-8 IN AJAX REQUIREMENT:

.ajax({
        async: false,
        type: "GET", 
        url: url, 
        contentType: "charset=utf-8", 
        success: function(data)
            { 
                $(".container").html(data);
            }
});

HEADER:

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

Make sure that all your files are saved as UTF-8 (UTF-8 or w.o BOM). Configure formatting in your IDE.

    
05.08.2015 / 16:39