Write/retrieve special characters ⓄⒼקร to Database [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
I have a form that needs to accept special font characters and write them to the database table. I believe the encoding is set correctly at the page/form level but when the field is written to the database the characters get changed to some other encoding. Other SO answers seem to indicate setting encoding to UTF-8 is the answer, which i've done.
Now, if I copy paste the characters below, direct to the database table, it holds them just fine as shown. Its only when I write it to the table from the form or when i retrieve it for display in web page.
Example characters: ⓄⒼקร
The web page is set as: <meta charset="utf-8">
The form tag includes attribute: accept-charset="UTF-8"
Php just before the INSERT has: $_POST['tag']=utf8_encode($_POST['tag']);
I have not had to write/encode those types of font/special characters before, so what am i doing wrong here?

Do not use the PHP utf8_encode() or utf8_decode() functions.
Despite their promising-sounding names, what these functions actually do is mangle UTF8 text -- either by double-encoding UTF8 text, or by converting text to the ISO8859-1 encoding and replacing characters outside the Latin-1 range with question marks.
Remove the call to utf8_encode(), make sure your database table has the proper encoding (CHARACTER SET = utf8mb4), and you should be fine.

Related

How do I echo a mysql result in UTF-8 into my page [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
I'm listing the customers names on my webpage with PHP retrieving data from MySql, but some of them are called José, João, but when I echo the result on the page, it shows Jos� and Jo�o.
The collations of the tables are all on utf8_general_ci what can I possibly be doing wrong??
This appears to be a browser display issue. Try adding the following line between the head tags of your html document.
<meta charset="UTF-8">
This should solve the problem. If not, leave a comment And I'll help you resolve it.
Your page source code should also be utf-8 encoded, to display UTF8 encoded characters, otherwise your browser tries to apply character encoding from http header to you strings.
use
echo utf8_encode($yourstring);
but, also is recommended to set in html
<meta charset="UTF-8">
and consider to create database with utf-8 encoding and collation spanish or whatever it have this kind of characters

php search function not displaying the correct encoding [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I'm building a php search page for a photo gallery to pull data with Chinese characters from MySQL.
I've done the following:
1. setting webpage to utf-8 encoding meta http-equiv="content-type" content="text/html";charset="utf-8"
2. ensure that the collation for mysql db and data itself is utf8_general_ci
3. set chrome encoding to utf-8.
But when I put in a positive search string to pull data with Chinese characters, they always come up as ???
I've checked my php array raw output and it is also ??? which leads me to believe there is something wrong at my database end. But I need to add is my gallery page (where the search box is embedded) displays the Chinese characters correctly from the same database.
Have you also set $mysqli->set_charset('utf8'); ?

Character encoding for music symbols in mySQL [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I am trying to store the characters ♯ and ♭ in a mySQL database, but ♯ gets stored as A♯ and ♭ as Bâ™
In the HTML, I have used ♯ and ♯ and both render fine in the browser, but neither is being stored correctly.
I have tried UTF-8 and UTF-16 character sets for both the PHP page and for the field where the values are being stored and I get the same result.
I'm not very familiar with character sets, so maybe I should be using something other that UTF-8 or -16 or maybe I'm missing something else entirely?
did you try to use varbinary / binary.
If you have again the problem, check your DB charset, table charset, col charset, php script charset.
If you have the problem after, you can use base64_(en|de)code :)

How are arabic letters (correctly) stored in mysql ? ب or ب [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
This might be a dummy question, but I'm a little lost in it.
How are Arabic questions exactly stored in a database ?
Let's take ب, if I insert that directly in the DB it becomes ?. Not good.
If I use a form (and php script) and store it as UTF-8, it is stored like ب. I can read it out and print it out, all good.
So my question is, are Arabic (and Japanese,...) letters always stored likes this in a mysql database ب ? Or should I change a setting somewhere and it should look like ب when I'm browsing the database?
It's just to define the length of my rows (varchars/chars) in the database...
DB set to utf8_general
Site fully UTF8
If you try to store a UTF-8 encoded character and it becomes ?, this means MySQL did not understand or support the encoding in which you sent the character. The column needs to be set to store utf8 data (better utf8mb4 if supported) and the connection encoding needs to be set to the correct encoding to inform MySQL in what encoding you're sending data to it.
If you get HTML entities from a form submission, this means the browser tried to send data in an encoding which did not support that particular character; therefore it had to fall back on HTML entities to encode the character. You need to set the encoding declarations correctly to tell the browser it should send UTF-8 encoded text to the server.
See Handling Unicode Front To Back In A Web App and/or UTF-8 all the way through for how to do all this.

Special characters setup PHP [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
Everytime I make a new project, I end up in having troubles because I forgot to create the database collation by UTF-8 or there are some characters that slipped trough that I didn't see like é/à/.. but also the double .. or tripple ... seem to be very nasty. I usually use mysqli_real_escape_string to make sure he writes the characters away, and when i print them i use htlmentities. But that doesn't work for all characters, and defenitly not for double .. or tripple ... .
Is there a general rule / guideline that I should keep in mind when setting up a project, so I don't have troubles with these special characters?
Is there a general rule / guideline that I should keep in mind when setting up a project?
Sure.
Always set your database connection charset to match your HTML page actual charset.
Say, your pages are in utf-8, then issue
mysqli_set_charset($conn,'utf8');
right after connect
of your pages are in Windows-1251, then make it
mysqli_set_charset($conn,'cp1252');
and so on
Also always use mysqli_real_escape_string to format string literals you're adding into query dynamically,
and use htmlspecialchars() when printing user input back to HTML page
Update:
Also you need to setup your tables with charset that supports all the required characters (UTF-8 is a preferred default).
CREATE TABLE `table` (
...
) DEFAULT CHARSET=utf8
when creating your tables with such definition you will never have ?s in your data

Categories