I'm coding a page that shows the user some information from database, the information contains some accents i make the request <input type="text" id="de" value="<?php echo $row->de; ?>" class="input" size="50"/>
here he shows problems with charset the word have ( í, ã ) and value shows ( ? ? ) not the word, how can i solve this ?
And yes i have <META http-equiv="Content-Type" content="text/html; charset=utf-8">
on my page.
Sorry for my bad english.
Asked
Active
Viewed 55 times
-5

user2942910
- 173
- 1
- 3
- 9
-
You're sure the database returns UTF-8 encoded material? – Kontrollfreak Apr 28 '14 at 15:55
-
the page might be set to utf, but are you SURE that your db tables and connection to the db are utf as well? The ENTIRE rendering pipeline must be the same charset, or connected by appropriate translation logic. any mis-match anywhere will trash things. – Marc B Apr 28 '14 at 15:55
1 Answers
0
Use PHP function utf8_encode
Try:
<input type="text" id="de" value="<?php echo utf8_encode($row->de); ?>" class="input" size="50"/>
Make sure that the IDE uses is configured as the default UTF-8.
This is spot-checking, ie the entire return must place this function. To correct definitive in check as below:
In every PHP output header, specify UTF-8 as the encoding:
header('Content-Type: text/html; charset=utf-8');
mysql_*
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8', $link);
mysql_* is deprecated
mysqli
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
$mysqli->set_charset("utf8");
PDO
$pdo = new PDO(
'mysql:host=mysql.example.com;dbname=example_db',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
On the MySQL side of things, modifications to the my.ini
file are required as follows:
[client]
default-character-set=UTF-8
[mysql]
default-character-set=UTF-8
[mysqld]
character-set-client-handshake = false #force encoding to uft8
character-set-server=UTF-8
collation-server=UTF-8_general_ci
[mysqld_safe]
default-character-set=UTF-8

Lucas Henrique
- 1,380
- 1
- 11
- 15
-
If `utf8_encode` fixes the problem, you'd be much better off fixing the rest of your data pipeline to/from the data store. There's almost never a reason to use `utf8_encode` in real life, unless you don't know what you're doing. – deceze Apr 28 '14 at 16:32
-
@deceze So I wrote to him to set him on the pages IDE by default UTF-8. His files are in another encoding for the simple fact of utf8_encode work. – Lucas Henrique Apr 28 '14 at 16:36
-
Nope, the file encoding has nothing to do with it. `$row->de` is Latin-1 encoded, and this seems to come from the database. The file's encoding is irrelevant for that. – deceze Apr 28 '14 at 16:40
-
@deceze I edited the post. Checks if everything is ok. Feel free to suggest edits. – Lucas Henrique Apr 28 '14 at 16:54