2

My problem is that what is written directly via PHP is correctly accentuated, but when the accentuated word comes from the MySQL, the letters come like this �.

I tried using the html charset as ISO-8859-1 and it fixed the MySQL letters, but broke the others. One way to fix it all is to set my .php files to ISO-8859-1, but I can't do it, I need to use it in utf-8 encode.

What can I do?


At the moment solution: Include mysqli_set_charset($link, "utf8"); before the queries (only need to do once for each connection made). I'm still looking for a conclusive solution on the server, not on the client.


EDIT:

mysql> SHOW VARIABLES LIKE  'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

mysql> SHOW VARIABLES LIKE  'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+

mysql> show variables like "character_set_database";
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| character_set_database | utf8  |
+------------------------+-------+
1 row in set (0.00 sec)

mysql> show variables like "collation_database";
+--------------------+-----------------+
| Variable_name      | Value           |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+
1 row in set (0.00 sec)

These are the values of my database, but I still cannot make it right.

EDIT2:

<meta charset="utf-8">
...
$con = mysqli_connect('localhost', 'root', 'root00--', 'eicomnor_db');
$query = "SELECT * FROM table";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['nome'] . "</td>";
    echo "</tr>";
}
mysqli_close($con);

Here's the PHP code.

Minoru
  • 1,680
  • 3
  • 20
  • 44
  • 1
    Use UTF-8 for everything. What character set does your database/table/column use? If this is not UTF-8, perhaps the best approach would be to convert it. – halfer Oct 09 '13 at 20:59
  • possible duplicate of [Mysql utf32\_unicode\_ci and html charset utf-8 used, but character � appear](http://stackoverflow.com/questions/11432758/mysql-utf32-unicode-ci-and-html-charset-utf-8-used-but-character-appear) – Sebas Oct 09 '13 at 21:06
  • I tried doing it, but I can't set the `character_set_database` and `character_set_server` to `utf8`. I already tried to use `set global character_set_server=utf8`, but it doesn't change. – Minoru Oct 09 '13 at 21:09
  • @Sebas In the other question, on a possible restart, the error comes back too, doesn't it? – Minoru Oct 09 '13 at 21:15
  • @LucasHarada no, i think it does not – Sebas Oct 09 '13 at 21:18
  • _I can't set the character_set_database and character_set_server to utf8_. Why not? Do you get an error, or is it just not possible given some design constraints in your system? That sounds like your problem right there. (I didn't see your message, btw: please use @halfer to ping me on questions/answers that are not mine). – halfer Oct 09 '13 at 21:18
  • @halfer I succeded on setting the variables to utf-8, but it's not working yet. – Minoru Oct 09 '13 at 21:40
  • Great. OK, so if you select a string containing an accented character **from your MySQL console**, does that work correctly? – halfer Oct 09 '13 at 21:44
  • @halfer Yes, it does. – Minoru Oct 09 '13 at 21:46
  • 2
    Cool - that means your database is fine. OK, so can you edit into your question the smallest possible PHP script in which the rendering error is exhibited? Consider your UTF-8 character set as declared in (a) your HTTP header, and (b) your meta tag. For most browsers, just one of these is sufficient to switch it to UTF-8 mode. – halfer Oct 09 '13 at 21:47
  • Check your connection is UTF-8 too, as @Kzqai suggests. – halfer Oct 09 '13 at 21:48
  • With PHP do you mean a CLI script or browser output (HTML)? – nietonfir Oct 09 '13 at 21:56
  • @halfer I edited with the PHP code. The charset meta header on the top is set too. – Minoru Oct 09 '13 at 22:08
  • 1
    Hmm, dunno. Try this, maybe, prior to the `SELECT` query? http://www.php.net/manual/en/mysqli.set-charset.php – halfer Oct 09 '13 at 22:23
  • _The charset meta header on the top is set too_. But it's not in your script; please add that in, both to your version and to the version in your question. – halfer Oct 09 '13 at 22:25
  • @halfer The `mysqli_set_charset($link, "utf8");` worked! I just wanna try to find how to configure it without needing to include it on every connection. Thanks anyways! – Minoru Oct 09 '13 at 22:31
  • 1
    Excellent news. It could be a configuration setting in your my.ini, but if it works, I'd leave it in there - better to run it pointlessly than to risk having the problem occur again. – halfer Oct 09 '13 at 23:33

4 Answers4

2

First off, don't try to modify your php files in the direction of ISO-8859-1, that's going backwards, and may lead to compatibility issues with browsers on down the line. Instead, you want to be following the path to utf-8 from the bottom up.

Good luck! charset issues with old databases are often more work than they initially appear.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • That's exactly what I'm trying to do. – Minoru Oct 09 '13 at 21:09
  • Check the edit, please. I did what you said, but it's still not working. – Minoru Oct 09 '13 at 21:42
  • That is potentially good, hopefully the charset mismatch is not in your database, which would be much easier to fix. Just make sure that your client connection is set to utf in your mysql .cnf: http://stackoverflow.com/questions/3513773/change-mysql-default-character-set-to-utf8-in-my-cnf And then run selects on the data in the database directly and see if the format matches. If you can see, for example the copyright symbol or the accented letter when selecting from the database, then that'd mean your data is fine. if not, the data may be garbled in your database despite settings for utf-8 – Kzqai Oct 09 '13 at 21:55
  • The data is OK. The `SELECT` shows them alright. But in the web ones are still messed. – Minoru Oct 09 '13 at 22:07
  • Ok, that suggests the candidates of: php's database connection. Apache server headers. Browser headers. Or html. – Kzqai Oct 09 '13 at 22:34
  • Try: `mysqli_query("SET NAMES 'utf8'");` (php connection) `mysql_set_charset('utf8');` (php connection) `ini_set('default_charset','utf-8');` (php code, shouldn't be this) `header('Content-type: text/html; charset=utf-8');` (html headers) In your code, in order, one at a time, and perhaps one of those factors will make the difference. – Kzqai Oct 09 '13 at 22:37
  • Well, like I commented to @halfer, adding `mysqli_set_charset($link, "utf8");` makes everything work fine. Do you know how to fix it without having to write it on every connection? – Minoru Oct 09 '13 at 22:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38932/discussion-between-kzqai-and-lucas-harada) – Kzqai Oct 09 '13 at 22:40
1

Have you tried iconv? As you know that the charset used on the DB is ISO-8859-1, you can convert to your charset (I'm assuming UTF-8):

// Assuming that $text is the text coming from the DB
$text = iconv("ISO-8859-1", "UTF-8", $text)
Murilo Vasconcelos
  • 4,677
  • 24
  • 27
0

Assuming you send the output to the browser, you need to ensure that the proper charset <meta charset="utf-8" /> is set and that you don't override it in your browser settings (check that it's either "auto" or "uft-8").

nietonfir
  • 4,797
  • 6
  • 31
  • 43
0

Include mysqli_set_charset($link, "utf8"); before the queries (only need to do once for each connection made) resolves the problem.

Minoru
  • 1,680
  • 3
  • 20
  • 44