Question mark showed instead of special character - php

I am fetching data from mysql server but the data contains some special character which when i try to print through my php code show a question mark instead. I know this has something to do with character encoding, and i have set charset to utf-8 in my html code but still I am not getting the special characters.

Add mysql_query("SET NAMES utf8") to the start of the php code after the db connection.

Related

MySQL won't store "€"

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

Getting mysql by php turns non-latin characters into question mark "?"

When I echo values with non-latin characters from MySQL they turn into question marks. And I mean question marks "?" not "�". I got these things:
header('Content-Type: text/html; charset=ISO-8859-2'); //php
<meta name="charset" content="ISO-8859-2" />//html
And they're not working!
Requesting help.
EDIT: More informations: in PHPMyAdmin I changed collation to utf8_polish_ci.
You might want to try issuing this SQL statement right after you connect:
SET character_set_results = latin2
It looks like your text is getting translated, by MySQL, from Unicode to latin-1 (iso-8859-1); the question marks you're seeing are replacement characters. MySQL translates text from its internal representation to the character set of the connection when it sends result sets.
You can read more about this here. http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

how to display this chinese character?

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.

Inserting into mysql database with asian symbols such as ’ —

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.

€ char is shown as ? in UTF8 Output

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.

Categories