1

I am reading a specific field from a database but I am having issues setting the correct charset, I still cant get the accents correctly (à, ã, ê, ...), it shows up as Chinese and question marks characters. is there something wrong in my php code? Thank you .

PHP

<?php
    require 'functions.php';
    require 'config.php';

    $con = connect($config['DB_HOST'], $config['DB_USERNAME'], $config['DB_PASSWORD'],'data_db');
    mysql_query("SET NAMES 'utf8'") OR die(mysql_error());
    $sql_textIntro = "SELECT * FROM INTRO_TEXT";
    $results_textIntro = mysql_query( $sql_textIntro, $con);
?>

HTML

<h1>TITLE</h1>
    <?php
        $record = mysql_fetch_array($results_textIntro);
        echo "<p>".$record['TEXT_FIELD'].'</p>';
    ?>
</div> 
Anju
  • 631
  • 2
  • 9
  • 25
user1765661
  • 585
  • 1
  • 7
  • 21
  • This function is very wrong - `mysql_query`. https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Michas Sep 21 '15 at 21:12

3 Answers3

2

Using mysql_set_charset is the right way to change the charset of the connection (not an SQL query).

You can do it this way:

mysql_set_charset('utf8', $link); //Take note that it's utf8 and NOT utf-8 here

You should also serve your webpage in UTF-8 and make sure your talble store strings in UTF-8 too.

Take note that functions mysql_* were deprecated in PHP 5.5.0, and were removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

AlexV
  • 22,658
  • 18
  • 85
  • 122
0

Try adding this as the first element inside the head tags: <meta charset="utf-8">

Here is a very good, basic, read on character sets by Joel Spolsky in case you want to understand what is going on :)

(See the section under "There Ain't No Such Thing As Plain Text.")

OskarD90
  • 613
  • 2
  • 8
  • 26
-2

in my.ini file must character-set-server = utf8 if does not helps then see:

have set in html head encoding? have apache set character set to utf8?

Kaxa
  • 515
  • 1
  • 6
  • 11