Saturday 29 October 2016

Getting special characters out of a MySQL database with PHP




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







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?


Answer



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'];
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...