Home > Mobile >  Problems with the output of icons in a mysql database stored text
Problems with the output of icons in a mysql database stored text

Time:12-18

I have problems with the output of icons within a text from a mysql database (InnoDB DEFAULT CHARSET=utf8) via php (online - PHP version 7.3.23).

Text like this:

Alles gut✅✅✅

In the phpMyadmin view (OFFLINE and ONLINE) they are displayed correctly to me. I also have no problems reading them in the script offline (PHP version 7.4.24) on the local server (Apache) in order to display them to me. Online with almost the same script I only get the pictures displayed as question marks. It is also interesting that I have to edit the text online with utf8_encode. Offline it just works without that to show me other special characters. This is absolutely the only thing in which the scripts now differ online and offline. What did I do wrong online?

Does anyone know what to do and can give me a tip? What do I have to consider? Is there a safe way to output it?

CodePudding user response:

When you're viewing the page "online", you're loading the page through HTTP; in addition to that, it's likely your script outputs HTML instead of plaintext (?). It's unclear.

Either way, HTTP adds 1 additional layer of text encoding into the mix (check the HTTP headers in your browsertools with F12 -> Network tab -> Refresh the page). HTML adds another couple layers into the mix, mainly within the <head> section. In both cases you want to see if any (text/character)encodings are mentioned, if it isn't UTF-8 (like your database), such as say UTF-16 or ISO-8859-1, then that may be the root of your problem.

Another possibility is that you've treated the text in a non-multibyte fashion since it was retrieved from MySQL. PHP unfortunately still has quite a few functions like that. An example of that would be if you used substr() instead of mb_substr(). You could rule this possibility out by printing the string value directly after it was retrieved from the database (before doing any manipulation or concatenation or the like).

Another option may be that you're actually describing a font issue instead of an encoding issue. Different fonts support different segments of unicode, so that could be your problem as well. PHPMyAdmin may well be using a different font than your webpage.

A fourth option is related to the database connection (in php). Databases define the type of character encoding at multiple levels: every database selects a charset*, every table, every char/varchar/text field individually; but (more relevantly), each connection has its own encoding. in your mysqli_connect/PDO::connect-style function, you could have another characterset selected.

* = As you had already determined yourself aswell, the problem cannot be with the database itself.

A fifth option is, basically identical to the fourth: it is possible that your database server has the wrong charset defined as per-connection default. In such a situation it could be that phpmyadmin defines its own per-connection charset (and thus overrules the mysql default), but your own website doesn't manually select one and thus gets the bad default. This option seems the least likely, but it is technically possible.

CodePudding user response:

I used to have them with me when I asked. It was definitely missing here, although the special characters were output without any problems. That was the solution apparently.

$ db-> exec ("set names utf8");

It definitely just works with this line. Maybe there are other options. Thank you very much for your answers. I'll probably take a closer look at all of this.

The only question that remains for me to understand is which Unicode or Html entity is behind it. Because they are shown to me in black and white instead of colored, for example in the PDF. For some I exchanged there with a picture. But I can't read others from the code. There is always the generated image or a question mark.

  • Related