Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting special characters out of a MySQL database with PHP [duplicate]

I have a table that includes special characters such as ™.

This character can be entered and viewed using phpMyAdmin and other software, but when I use a SELECT statement in PHP to output to a browser, I get the diamond with question mark in it.

The table type is MyISAM. The encoding is UTF-8 Unicode. The collation is utf8_unicode_ci.

The first line of the html head is

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

I tried using the htmlentities() function on the string before outputting it. No luck.

I also tried adding this to php before any output (no difference):

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

Lastly I tried adding this right below the initial mysql connection (this resulted in additional odd characters being displayed):

$db_charset = mysql_set_charset('utf8',$db);

What have I missed?

like image 280
Jason Wood Avatar asked Apr 09 '13 03:04

Jason Wood


1 Answers

Below code works for me.

$sql = "SELECT * FROM chartest";
mysql_set_charset("UTF8");
$rs = mysql_query($sql);
header('Content-type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($rs)) {
    echo $row['name'];
}
like image 121
Tian Yong Avatar answered Sep 29 '22 16:09

Tian Yong