user3423076
user3423076

Reputation: 53

PHP and Mysql special chars

i haven't found the answer at this question browsing around so i guess asking it's ok...

My php code reads from my Mysql Database a string and prints it, here is the code

$sql2=mysql_query("SELECT * FROM Corsi WHERE Nome='$Ln_1[Ln_1]'");
$Ln_1_a = mysql_fetch_array($sql2);
$Ln_1_descr = $Ln_1_a[5];

But special chars, such as 'è' 'à' 'ò' etc. are printed as '�'. My Mysql Encoding is utf8 and also in my html header i have utf8 encoding, so what is wrong with this?

Thanks in advance

Upvotes: 2

Views: 165

Answers (4)

Lkopo
Lkopo

Reputation: 4825

First of all, at the start of your MySQL connection after database selection you should put this query:

mysql_query('SET NAMES utf8');

Then be sure, your page has encoding UTF-8 (I guess you are using HTML, so it should looks like:

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

) and also your script file must be saved in UTF8 encoding.

But I have to remind mysql_* functions are deprecated and you should use mysqli_* or PDO instead with prepared statements due to safety of your queries.

Upvotes: 2

Bear 곰
Bear 곰

Reputation: 35

Use htmlspecialchars($yourvariable, ENT_NOQUOTES, "UTF-8")

Also verify your MySQL configure file has:

[client] default-character-set=UTF-8

[mysql] default-character-set=UTF-8

Also, utf8_encode() and utf8_decode() would work for you as well.

Upvotes: 0

Wezy
Wezy

Reputation: 667

Use the utf8_decode function

like this

$Ln_1_descr = utf8_decode($Ln_1_a[5]);

Upvotes: 0

Mike S.
Mike S.

Reputation: 2111

Try (after mysql_connect) do mysql_query('SET NAMES utf8'). And also check, if font support these characters.

Upvotes: 1

Related Questions