What is the best Collation for the column that can allow to store accented letters and parse them out perfectly without any encoding error, because whenever I add an accented letter such as é, å, it shows out with an encoding problem on the PHP side, but in the MySQL side it's fine...
How do I get the accented letters display properly?
You get them correctly by matching the encoding on both ends, ie. both your PHP output and your DB should use the same encoding. For European languages I would suggest using UTF-8 for both your scripts and the DB. Just remember that you still have to initialize UTF-8 collation in MySQL using SET NAMES 'utf8' COLLATE 'utf8_general_ci' (so run this query just after you make a connection to the DB and you should be ok).
Perhaps your problem isn't within the database, but within however you're displaying things from PHP? What content encoding are you specifying in your output? You might need to manually send a header to specify that the content is UTF-8 if that's what you're trying to output.
For instance: header("Content-Type: text/html; charset=UTF-8");
Related
I am trying to upload text into my database. I keep getting encoded text when user input certain characters. For example, if the user inputs "Jalapeño" I end up getting "Jalape%C3%B1o" in my database.
I know this is because the "ñ" is being read as "%C3%B1"...
I am not exactly sure how to go about converting all of the potential accented characters, I have looked at rewurldecode and others, but am wondering if there is a quick solution here. Any recommendations would be wonderful. Thank you!
Make sure your database uses the right character set and collation (usually you'll want UTF-8). Also make sure you you set the connection character set and sent the right headers.
e.g. mysql:
mysql_connect();
mysql_set_charset("utf8");
header('Content-Type: text/html; charset=utf-8');
You could also use htmlentities to convert the special characters into HTML entities, but you'll still need to make sure your database collation and connection settings are correct.
Your character is urlencoded. To display it properly, you have to decode it. But url encoding is not meant to be used when saving data in the database. As dtech points, you have to use utf8 character set.
I am storing Unicode text لاہور in MySQL, I have set tables and columns to utf8_general_ci. The text لاہور is displaying correctly in MySQL. However if I echo that with PHP it shows ?????? on the browser window.
One thing to mention here: I have the whole document in Unicode and all words are displaying correctly, but they are written directly i.e. not coming from MySQL.
Even if I try
$p="لاہور";
echo $p;
It displays لاہور in the browser. Things go wrong only when retrieving from MySQL.
One common cause for this is that your PHP script is being saved with another format (for example ASCII), you must be sure that your PHP script is also saved as UTF-8 or whatever codification you use in your database.
Another possible cause is that MySQL is not returning proper Unicode characters to your script, you may use mysql_query("SET NAMES utf8") or whatever encoding you want to use, before processing your queries, a good way to troubleshot this problem could be converting the string to their respective unicode codes and comparing them to see if they're the same.
It may not always be sufficient to set the content type using meta tags, I usually set it via the header directive as well as below.
header('Content-Type: text/html; charset=utf-8');
Most likely your MySQL connection (as opposed to storage) has not been set to UTF-8, causing the UTF-8 data retrieved from MySQL to be converted to Latin1 (or similar), which cannot represent those characters and they are replaced with a ?.
If you are using mysql_:
mysql_set_charset( 'utf8' );
If you are using mysqli_:
$mysqli->set_charset( 'utf8' );
before you make any queries
If you are using PDO, add charset=utf8 to the connection string.
There is a MySQL database and I want to select columns from a table.
I must return a String from the concatenation of the selected column values. But in one of the columns there are accentuated letters in the column value , like é.
So how to encode the column returned value ?
NB : I already wrote header('Content-Type: text/plain; charset=utf-8'); at the beginning of the PHP file.
Defining charset with SET NAMES 'utf-8' may help.
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
What encoding is your database table in? On a lot of installations, MySQL defaults to LATIN-1. Make sure the table stores its data as UTF-8, then make sure that the connection between MySQL and PHP is in UTF-8. The easy way to do that is running the query SET NAMES utf8 after connecting, but you can also set a default encoding.
Next, the UTF-8 header should be sent from the server to the browser, but you've already done that by adding the header() call.
If your database table is currently not encoded as UTF-8, you might need to re-enter your data after changing it.
Maybe the Multibyte String Module can be of some help to you, part of the Human Language and Character Encoding Support.
I'm currently writing a php5 web application. It suppose to extract data from the DB and show it in an HTML page. In order to show Hebrew characters, the charset encoding in the HTML is:charset=windows-1255
The application is configured to work with MySQL.
I have problems with the Hebrew encoding, it's actually some kind of paradox...
1. When I configure the collation in the DB table to be UTF_bin and the charset UTF-8:
The Hebrew characters that extracted from the db are shown fine, the HTML Hebrew not.
2. When I configure the collation in the DB table to be UTF_bin and the charset windows-1255:
The Hebrew characters that extracted from the db are show are show as random characters, the HTML Hebrew is ok.
Does anyone has an idea how to solve it?
Thanks!
Do you have a simple coding sample/example?
I assume you've..
1) appended the charset on the end of the HTTP Content-Type header too?
header('Content-type: text/xhtml; charset=windows-1255');
as well as the tag?
2) using multi byte string functions http://php.net/manual/en/book.mbstring.php or are overloading all string functions http://www.php.net/manual/en/mbstring.overload.php, and/or hebrev() or hebrevc() where required
3) and you're using setlocale() for other localisations http://php.net/manual/en/function.setlocale.php
If you're pulling UTF out of the DB, you may need to use iconv() or similar to translate it to another encoding?
http://www.php.net/manual/en/function.iconv.php
I am facing a paradox in decoding with utf8_encode decode. I have a MySQL database with uft8 collation and whose fields have the utf8_general coding. I have my php file in utf8, and in my HTML pages I have specified in the header the utf8 charset.
My problem is that when I select from my table a field containing accented characters (like èçò ùé) and echo that to the browser, I get strange characters.
To resolve my problem, I have to echo $description=utf8_encode($imm['description']).
My question is why can’t I do the echo directly without having to use uft8_encode every time?
I'll just guess that your database connection is not set to UTF-8.
See SET NAMES utf8 in MySQL?
you need to specify the header using php to be utf-8. also make sure that the format of the chars is utf-8 before storing in the db because utf_encode encodes an ISO-8859-1 string to UTF-8, which most likely means that the chars are being stored as ISO-8859-1 in s a utf-8 table.
make sure that you convert those chars in utf-8 before storing them in the db and then echo should not be a problem at all.
Source: had the exact same problem myself.