I am working on a application where I need to store/show data containing many special characters. I have set database collation utf8. I have set collation of table utf8 and character set as utf8_unicode_ci. It is storing all special characters like é, â. But whenever a character ,€ comes it isn't stored as it is. Like whenever there is a word “attributed†it becomes âattributedâ. I am currently using Laravel 5.2 (PHP) .
What I have tried so far
I have set following in my code
iconv_set_encoding('internal_encoding', 'UTF-8');
mb_internal_encoding('UTF-8');
I have also tried
$value = array_map("utf8_encode", $array);
But this special character isn't getting stored as it is. Will any one let me know what should I do to get this special character saved as it is.
try setting your collation to "utf8_general_ci" in your mysql
Normally its no problem to store the € sign to a database field. Check if all your scripts are in a correct coding.
Set your table and all data in that table to utf8_general_ci then try to change your php file to UTF-8.
header('Content-Type: text/html; charset=utf-8');
And if you have incorrect data then use.
utf8_encode("test");
to encode your string to a correct UTF-8 string. If that isn't working i think your data or string your try to convert is not correct.
For being able to use UTF8 characters in your queries you must run a certain query setting this, just like this:
$db->query("SET NAMES UTF8");
But I've seen that you said you're using Laravel. I haven't been working on it, but I guess this's automatically set by the charset parameter written in config, just like in the screenshot of this question:
Laravel UTF-8 To Database
Related
I am trying to replace £ with £ and it did not work.
I've tried:
echo str_replace("£", "£", "£3 Discount Discount");
I have also tried html_entity_decode which also did not work.
This is an issue with trying to display UTF-8–encoded data as non–UTF-8. You need to make sure that all character encodings are consistent, and if not then you're converting between them appropriately. The easiest way is to ensure that absolutely everything is in UTF-8. This includes:
The data that's saved in the database (MySQL's character set / collation)
The client connection to the database (Using SET NAMES UTF-8)
The output to the browser (header('Content-Type: text/html; charset=utf-8');)
The PHP script containing the code (yes, this sometimes has an impact)
I would first suggest checking that there isn't any mojibake in your database (e.g. using phpMyAdmin or command-line client), before checking the character sets above. If you find that the database actually contains £, then I would suggest applying the same logic above to any input mechanism to the database (including character encoding of HTML forms).
(Note: I've assumed MySQL throughout this answer.)
If you're able try and use £ instead of the £ character and save yourself the trouble.
You can try cleaning it up in the DB instead. Adapt this query to suit your needs.
UPDATE YOUR_TABLE_NAME SET THE_ROW = REPLACE(THE_ROW , '£', '£');
Try utf8_decode() instead.
I already set mySQL Collation as utf8_unicode_ci, if I manual insert chinese character in my database, it's successful work to displaying chinese character, but once i use my code, it was display this
ã€å¼µç‘žæŒ¯ã€æ±Ÿç¥¥ç¶�..
I had add this <meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta> in my header, I had try utf8_encode ,but still the same problem happen.
thank you and hope you guy reply me soonest
Have you set the connection character set / collation? Execute this query immediately after creating a connection
SET NAMES 'utf8'
You have probably forgot to execute this query after connecting
SET NAMES utf8
Try it and you'll see
there 2 steps you should do
add a query with UTF8
$query = "SET NAMES 'utf8'";
mysql_query($query);
make sure your file is encoded as UTF8,
open your script with your favourite editor and save as UTF8
To ensure MySQL expects UTF-8 encoding by default from client connections, use the following query:
SET NAMES 'utf8'
In addition, make sure PHP interprets the string as UTF-8 string. Since PHP does not support multibyte characters, you must use a function to allow PHP to work with UTF-8 strings:
utf8_decode()
or something.
I have a table named "cust_details" which has a column "categories", where I have to store some categories like : blockadenlösung, affirmation, beziehungsprobleme lösen
But when I am trying to save this data into the database it is stored like :
blockadenlüsung, affirmation, beziehungsprobleme lösen
That is when umlauts are coming in the string it is not saved in its original form. I tried some charset for storing this characters. But I am still facing the problem.....
What may be the possible reasons...?
Thanks In Advance.....
The data you stored is encoded in UTF-8 (ü for an "ö" is typical for UTF-8), but is not displayed as UTF-8 but rather as ISO-8859-1 or the like.
Make sure that you use the same encoding everywhere:
Deliver your websites with Content-Encoding "utf-8"
Use mysql_query("SET NAMES 'utf8'"); to set the encoding to utf-8
Make sure that the encoding of the database is UTF-8 (use HeidiSQL etc. to check)
Use this when you are inserting the characters:
N'characters here'
The N before the string declaration should enable you to enter it into the DB.
What is the type of the field?
You could specify database/table/field level character-sets. The default latin-1 works in most scenarios.
Otherwise, you would have to use plain text and store unicode strings like &#<4-digit-unicode-value>; into it. Then when you print it out, just dump the unicode into HTML and it will show up as such.
Here is a sample string in Pashto ترافيکي پيښو کې درې تنه مړه او څوارلس نور ټپيان شول. which we store directy into the table. The charset used is latin_charset_ci
Good Luck!
I cant seem to get these Chinese punctuation marks to work with my database (utf-8)
when i do an echo of the query the marks look like this
���
in php i have already done
$text=mysql_real_escape_string(htmlentities($text));
so as a result they are not saved into the database correctly what can i do to fix this?
Thanks
Executing mysql_query('SET NAMES utf-8'); before any operations with unicode will do the trick
Try using using utf8_encode() function while inserting into db and utf8_decode() while printing the same.
Add the character 'N' before your string value.
Eg. select from test_table where temp=N'unicode string'
besides if you want to use htmlentities, you have to set it to utf-8 encoding like that:
htmlentities($string,ENT_COMPAT,"UTF-8");
Don't put HTML-encoded data in the database. It should be raw text until the time you spit it onto the page (at which point you should use htmlspecialchars().
You need to make sure that both your database and your page are using UTF-8:
ensure your tables are CREATEd with a UTF-8 collation;
use mysql_set_charset after connecting to ensure the connection between MySQL and PHP is UTF-8;
set the Content-Type of the page to text/html;charset=utf-8 by header or meta tag.
You can get away with using a different encoding such as the default latin-1 on the database end and the connection if you treat it as bytes, but case-insensitive comparisons won't work if you do, so it's best to stick to UTF-8.
I have reworked a website and now it is xhtml valid etc and using UTF8. Everything is fine, but if anywhere in the Database is a Euro-char it is just displayed as a questionmark.
What would be the right way to fix this?
As output is done by Typo3 i cant change much about that.
Try executing these queries before the queries that fetch data:
SET NAMES utf8
SET CHARACTER SET utf8
This might be due to wrong database connection encoding
Lookup SET NAMES sql statement
$db_link = mysql_connect($host,$user,$passwd);
mysql_query("SET CHARACTER SET 'utf8';", $db_link);
mysql_query("SET NAMES 'utf8';", $db_link);
DON'T issue both statements!
Don't issue
SET NAMES utf8
SET CHARACTER SET utf8
one after another. It can cause trouble. I already had bad experience with SET CHARACTER SET utf8 right after SET NAMES utf8.
I recommend to issue only SET NAMES ...
MySQL docs has explanations why: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
In short: SET NAMES ... sets connection's charset to the same as client ans result charset. while SET CHARACTER NAME... sets different connection's charset and collation.
Please read the doc and decide whichever it better in your case. Or even better make a test.
What charset (encoding, collation,...) are you using for the database column that contains the € sign?
The problem could be that your data stored in this column ist mixed up because the € sign is a somehow difficult beast when not using UTF-8 character encoding. The problem ist that the € sign is encoded as \xA4 when using ISO-8859-15 and as \x80 when using Windows-1252 (the common Western-European charset on Windows machines).
If your data inside the column is not encoded correctly MySQL won't be able to transcode it into UTF-8 correctly - even if you use SET NAMES utf8.
If you're sure you have the right character data (0x20AC), it could also be the fonts ont he client-side. If the font you are using does not handle that particular character, you'll just see some default question mark.
However, why not use the escape code €, which gives you: €
Cheers,
You could try something like
$value = iconv('ISO-8859-1', 'UTF-8//TRANSLIT', $value);
The "ISO-8859-1" part may be different depending on your MySQL table character encoding.