简体   繁体   中英

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

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.

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

Use the utf8_decode function

like this

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM