1

I have a database with utf_general_ci encoding in MySQL, when I insert data in Navicat or PhpMyAdmin and do queries there, the special characters are returned fine.

But in a php variable they are showing like this:

how they show

My database and collations:

database

and in my html doc if it is a normal

it shows the characters OK, it only not work when is a php variable

please help me

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mariana Hernandez
  • 1,919
  • 4
  • 19
  • 29
  • 2
    show us how you get these rows and how used php variable? i guess that you not did `"SET NAMES UTF8"` or `charset=UTF-8` – Jason OOO Oct 09 '13 at 20:18
  • you need to have the SAME encoding throughout, including on the html page itself. – Marc B Oct 09 '13 at 20:18
  • look into `htmlspecialchars` http://www.php.net/manual/en/function.htmlspecialchars.php and `htmlentities` http://www.php.net/manual/en/function.htmlentities.php – cmorrissey Oct 09 '13 at 20:21
  • if i do set names utf8 it works, but ive never done that before and was working.. – Mariana Hernandez Oct 09 '13 at 20:22
  • `` where i print it and `$rows = array(); $res = mysql_query('SELECT * FROM usuario u, rol r WHERE u.rol_id=r.rol_id'); while ($row = mysql_fetch_array($res)) { $rows[] = $row; } return $rows;` @JasonOOO – Mariana Hernandez Oct 09 '13 at 20:24
  • http://stackoverflow.com/questions/2159434/set-names-utf8-in-mysql – Jason OOO Oct 09 '13 at 20:32

3 Answers3

4

Try:

$variable = utf8_encode($valueCommingFromDB);
1

It's important to note that utf8 in mysql is fairly limited, especially utf8_general_ci. You might want to switch to utf8_unicode_ci or utf8mb4

What's the difference between utf8_general_ci and utf8_unicode_ci

http://mathiasbynens.be/notes/mysql-utf8mb4

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
0

Hei,

Firstly be sure you have this meta in html tag

<meta charset="UTF-8">

You can check also with this line in php, above you mysql extraction

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

If you store your special character in database as html code like this &#39; which is ' when you extract try to use this php function

htmlspecialchars_decode($your_text_from_db);