PHP/MySQL Special Characters aren't displayed properly [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special characters in PHP / MySQL
I have a problem. I have a piece of text in my database (MySQL 5.5.20) with characters like 'é' and " ' " who aren't displaying properly after executing the MySQL query and displaying it with echo($...). With every special character I've inputted in the database, it displays a small question mark inside a diamond. If I look at the text in the database itself, it is an 'é' and " ' ", so I figured the problem isn't MySQL.
One thing I could do is str_replace everything like " ' " --> "'" on input, but then I have to do this for every character there is.
Oh and I already have included
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and this didn't work.
Hopefully you've all the information to help me, if not just say :) Thanks in advance!
Milaan

You need to have everything in utf-8:
The database field
The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
The content type (like you have already)

I was using the SQL query SET NAMES utf8 right after the connection to a DB is done successfully for over a years.
But this is not neccessary when You have everything in the same encoding
source files encoding
table columns collations
web page encoding (both in PHP header('Content-Type: text/html; charset=utf-8'); and in <header> <meta name="Content-Type" value="text/html; charset=utf-8" />)

I usually format all the input text with str_replace an replace all uncommon symbols with their &#xxx; equivalent, this is actually useful to prevent injection and bad html rendering
i.e. if someone inputs html tags they'll be active in your page and so on.

Related

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

Em Dash and En Dash from MySQL Db with PHP

I'm trying out output a MySQL database field with PHP which contains em dash and en dash, but although the ROWs output, the values with these dashes do not.
As far as I am aware, these are characters which should be used in proper english, therefore I don't think I should be stripping them out or replacing them with an alternative (like a hyphen).
By adding this code before the INSERT, I am able to get the em dash and en dash into the database properly (whereas without this line I saw unwanted characters instead):
mysql_query('SET NAMES utf8');
But, the value won't output. The database table and it's fields are using the utf8_general_ci collation and I've got these lines in my PHP page:
header('Content-type: text/html; charset=utf-8');
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I'm outputting the value like so:
echo nl2br(htmlentities(preg_replace("/[\r\n]+/", "\n\n", $row['someText'])));
If I output the value without formatting, I see this question mark character:
�
Does anyone know how to get around this? Am I forced to replace them with hyphens even though that's grammatically incorrect, or is there a way to output them as they appear in the database?
I added the same MySQL command before retrieving data from the Db and this solved the issue.
mysql_query('SET NAMES utf8'); // Use utf-8
I don't know why this is needed but I'll investigate and see if I can make this a default.

Showing � characters in place of ' during submit through form in MySQL and also removing new line in text? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UTF-8 all the way through
I want submit text from text box in a form to a table in MYSQL which in turn show text in the post on website but there is a special quot ‘ in place of this ' which get converted into Â
Please tell how to submit data with simple text without showing these characters
I have already used
urldecode ( string $str ) , str_replace ()
Here is my code
$description = mysql_real_escape_string(htmlspecialchars(utf8_encode($_REQUEST['txtDescription'])));
You should call SQL code
SET NAMES utf8
before DML or SELECT.
Then make sure your HTML is utf8 encoded:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Today I found answer of my question by doing deep study on net
Strange special characters shown in my question are shown due to meta tags
so when we add UTF-8 meta then such type of characters are show so here is the solution
Replace below code on your webpage
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
with
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
Showing all code required to insert, retrieve and showing on webpage without showing any special characters
During submit of text to MYSQL table using query use below code
$description = $_REQUEST['txtDescription'];
$description = mysql_real_escape_string($description);
$description variable will be used in SQL insert query
During Retrieve and Showing of text from MYSQL table use below code
<?php print(str_replace("\n", "<br />", $row['tDescription']));?>
And also follow the 1 Answer above to show it properly

PHP & mySQL - ë written as ë [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP messing with HTML Charset Encoding
We've come across special characters being transformed.
What is causing this? How can we fix it?
For example:
ë becomes ë
Thank you.
Thats a utf-8 character and you can parse it through utf8_encode() and utf8_decode() in PHP
Charset can be set at numerous places.
table charset
field charset
PHP-MySQL connection charset
Apache default charset
and in HTML metainfo
Make sure you use UTF-8 everywhere, and don't forget to setup the connection properly before the first query:
mysql_query("SET NAMES 'utf8'");
Make sure you are setting your charset in HTML document and with PHP header's function.
Also, you could try to make the first query in MySQL to be SET NAMES=UTF8 (SET NAMES utf8 in MySQL?)
If this is the output of a PHP script, I guess you may consider mb_internal_encoding() function.
Or you can fix that by HTML encoding meta tag. Like <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> inside <head>...</head>.

Help needed in choosing a function in PHP

Well i am trying to make XML from database..but one the field has the following value
Noobé
As you can see there is small dash like above 'e'
the output i obtained is Noobe?
I tried to utf8_encode the field but it does not work
I even tried to utf8 in header it did not work..
Can u guys suggest me a function by which i can overcome the above problem..
I also have a similar problem regarding a '–' obtained by Microsoft word
Help Appreciated..
I had a problem with accented characters not long ago. What worked for me was to add this line after my PHP code used to connect to the database :
mysql_query("SET NAMES 'utf8'");
What is the encoding of the HTML page?
I have a french website that have this in the header:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
And I can see all é à and others characters.
The problem is not encoding, as the "é" will become something like "À€". You may have something in you code that remove accents, maybe like http://php.net/manual/fr/function.strtr.php

Categories