3

I am a starter in php/mySQL, and I am currently facing a problem to display symbol such as ® onto my html. The symbol is stored in a table which can display properly when viewed from phpmyadmin, but when I use php to retrieve the table content, it does not display the symbol but instead displaying a symbol of a diamond with a ? inside it. I have set the html page to utf-8 and my table to utf8_general_ci but no luck from those.

The symbol is able to display correctly when I put straight to html or even store in php variable.

The query I used to get the content is

while ($row = mysql_fetch_array($result)){

echo ($row["symbol"]);

}

Many thanks in advance

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
HSL808
  • 41
  • 2
  • 6
  • When you say you set the HTML page to utf-8 did you set the encoding of the file and/or ? – Tom Tom Mar 01 '15 at 06:26
  • possible duplicate of [Charset=utf8 not working in my PHP page](http://stackoverflow.com/questions/10460349/charset-utf8-not-working-in-my-php-page) – gknicker Mar 01 '15 at 06:30
  • How do you set the encoding and have you verified via the response headers that it is being served as utf8? – chris85 Mar 01 '15 at 06:49
  • While these other suggestions should 'fix' the cause of the issue. After you change your SQL charset. You need to delete the bad character and type it again directly into the row. I used to have those little diamond question marks and I had to delete that character from the value stored in SQL, type it in again from the keyboard, and re-save the row. – FactoryAidan Mar 01 '15 at 08:44
  • I have set in the head – HSL808 Mar 01 '15 at 11:12
  • The retyping of the symbol was no luck, thanks tho aidan – HSL808 Mar 01 '15 at 11:12

3 Answers3

1

You can use html character entities instead of direct symbol

Do not use

®

Try it

®
Nahid Bin Azhar
  • 703
  • 1
  • 5
  • 18
1

These types of encoding issues can get complex when dealing with different character sets. In these cases, just changing the collation will not fix the problem, you need to change the CHARSET. Only after changing the CHARSET should you worry about the collation (they are not the same thing).

Just to be safe, export your database/table before altering it.

I would begin, by converting the table to utf8 since it is now the standard.

ALTER TABLE tbl_name
CONVERT TO CHARACTER SET utf8

By doing this, it will also change the CHARSET of the table and columns to utf8, but you may still need to manually change the collation of the columns to utf8_general_ci (seems like you have already done that).

In the event you want to change the default character set (for new columns)...

ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8

EDIT :
If changing the CHARSET in the database doesn't work, you can try setting it on the PHP side. Just add this after your connection.

mysql

mysql_set_charset("utf8");

mysqli

$mysqli->set_charset("utf8");

PDO

PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET 'utf8'"

Here is some helpful documentation:

10.1 Character Set Support
10.1.12 Column Character Set Conversion
10.1.13.1 Unicode Character Sets

EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • Thanks EternalHour, I have converted the character of table to utf8 as you suggested, still displaying a diamond with ? inside it – HSL808 Mar 01 '15 at 11:16
  • @HSL808 - I've added something else to try. – EternalHour Mar 01 '15 at 19:53
  • The $mysqli->set_charset("utf8"); Is what ended up working for me. My table is also converted and the charset of the field as well as I tried everything. – JSG May 21 '21 at 14:23
0

You can convert the trademark, copyright or other symbols into/out database via an HTMLEntity

The htmlentities() function converts characters to HTML entities.

Reference: http://www.php.net/manual/en/function.htmlentities.php

Reference: http://www.php.net/manual/en/function.htmlspecialchars.php

® Registered Trademark ®

™ Trademark Symbol: ™

Other useful information and symbols can be found here: http://www.w3schools.com/html/html_entities.asp

mg33dev
  • 148
  • 1
  • 19
  • This has nothing to do with why the charset fails to output symbols when retrieving a MySQL Query when the symbol is fine inside the DB. – JSG May 21 '21 at 14:26